Oracle SQL Queries
Oracle SQL Queries
Example
Update supplier1 set suppliername = ‘bcd’ where supplierid = ‘1’;
****************Query to add column in table********************
Syntax
ALTER TABLE table_name add columnname datatype(size);
Example
Alter table supplier1 add supplieremail varchar2(255);
****************Use of Distinct********************
Select distinct suppliername from supplier1 where supplierid = ‘1’;
*******To find ascii value corresponding to a particular character******
Select ascii(‘A’) Big_A, ascii(‘z’) small_z, ascii(‘ ‘) from supplier1;
Output
***********Use of LPAD************
LPAD(c1, i [,c2]) takes three arguments, where c1 and c2 are character
strings and i is an integer.
This function returns the character string c1 expanded in length to i
characters, using c2 to fill in space as needed on the left side of c1.
Example
select lpad(suppliername, 10) lpad_lname, lpad(suppliercity,8,1)
lpad_suppliercity from supplier1where suppliername like 'd%';
*********Use of Ltrim**********
LTRIM(c1 [,c2]) takes two arguments, where c1 and c2 are character
strings.
This function returns c1 without any leading characters that appear in c2.
If no c2 characters are leading characters in c1, then c1 is returned
unchanged.
c2 defaults to a single space.
Example
Select ltrim(‘Mississippi’, ‘Mis’) from supplier1;
********Use of Replace********
REPLACE(c1, c2 [,c3]) takes three arguments, where c1, c2, and c3 are
character strings.
This function returns c1 with all occurrences of c2 replaced with c3.
Example
select replace ('uptown', 'up', 'down') from supplier1;
**********Use of RPad***********
RPAD(c1, i [, c2]) takes three arguments, where c1 and c2 are character
strings and i is an integer.
This function returns the character string c1 expanded in length to i
characters, using c2 to fill in space as needed on the right side of c1.
*********Use of Substr*********
select substr('The Three Musketeers', 1,3)Part1, substr('The Three
Musketeers',5,5)Part2, substr('The Three Musketeers',11)Part3, substr('The Three
Musketeers',-5)Part4 from salesstore1;
*********Use of nvl***********
The NVL function is used to replace a NULL value with a literal value.
NVL takes two arguments, NVL(x1, x2), where x1 and x2 are expressions.
The NVL function returns x2 if x1 is NULL.
If x1 is not NULL, then x1 is returned.
For Example:
Select NVL(100,200) from dual; returns 100(literal value)
Select NVL(NULL, ‘abc’) from dual; abc
Select NVL(‘abc’,NULL) from dual; abc
********Use of translate********
*********Use of coalesce*********
If x1 is not NULL, then
Return x1
Else
Check x2
If x2 is not NULL, then
Return x2
Else
Check x3
If x3 is not NULL, then
Return x3
Else
Return NULL
End If
End If
End If
***********Use of BITAND************
*********Use of Ceil*********
SELECT CEIL(9.8), CEIL(0), CEIL(5) FROM supplier1;
*********Use of Floor********
SELECT FLOOR(9.8), FLOOR(137) FROM supplier1;
********Use of Round*******
select round(123.489), round(123.489,2) from supplier1;
*********Use of Modulus********
select mod(14,5) from supplier1;
*********Use of SQRT*********
select sqrt(64), sqrt(49), sqrt(81) from supplier1;
*********Use of truncate********
select trunc(123.489), trunc(123.489,2), trunc(123.489,-2), trunc(1275,-2) from
salesstore1;
********Use of Sysdate*********
select sysdate from dual;
********Use of timestamp*********
select sysdate, systimestamp from supplier1;
********To add months*********
select sysdate, add_months(sysdate, -1) prev_month, add_months(sysdate,12)
next_year from dual;
*******Adding current date********
select sysdate, current_date from dual;
*******Adding Timestamp******
select current_date, current_timestamp from dual;
******Extracting year from sysdate*******
select sysdate,extract(year from sysdate) year from dual;
*******SessionTimeZone*******
Select SESSIONTIMEZONE FROM dual;
*******To extract last day and next month last day******
Select sysdate, last_day(sysdate) end_of_month, last_day(sysdate)+1
next_month from dual;
*****To select next day from current day*****
Select sysdate, next_day(sysdate,’Fri’) from dual;
*****To round nearest time*****
Select sysdate, round(sysdate,’HH24’) from dual;
*****Conversion from binary to number*****
Select bin_to_num(1,1,0,1) bitfield1, bin_to_num(0,0,0,1) from dual;
Pending
Canceled
Output
ServiceId Seller_name Purchasername PlotNo
1001 Big Show Sam 12
1002 Gem Pill 13
1003 Matt Don 14
1004 Null Brock 15
Output
ServiceId Seller_name Purchasername PlotNo
1001 Big Show Sam 12
1002 Gem Pill 13
1003 Matt Don 14
***Full Outer Join Example***
Select serviceid, sellername, purchasername, plotno from purchaser full outer
join seller on purchaser.serviceid = seller.serviceid;
ServiceId Seller_name Purchasername PlotNo
1001 Big Show Sam 12
1002 Gem Pill 13
1003 Matt Don 14
1004 Null Brock 15