Anda di halaman 1dari 13

SQL QUERIES

create table employ (eno numeric(3), ename varchar(20), sal


numeric(5), desg varchar(8), deptno numeric(3), comm numeric(5),
jdt datetime, hdt datetime);

insert into employ values (101,'anil',4500,'sm',10,390,'12-01-


2005','11-01-2006')
insert into employ values (102,'bhanu',5000,'sm',10,350,'01-02-
2004','02-03-2005')
insert into employ values (103,'charan',30000,'md',100,0,'02-05-
2004','01-03-2005')
insert into employ values (104,'danu',15000,'hr',40,null,'03-02-
2004','04-01-2005')
insert into employ values (105,'eswar',8000,'ja',20,null,'01-02-
2002','01-01-2003')
select * from employ
select eno,ename,desg from employ
select ename,deptno from employ where deptno='10'
select ename,(12*sal)+comm 'annual salaray' from employ
select ename,desg from employ where desg='md'
select ename,sal from employ where sal>10000

create table employ (eno numeric(3), ename varchar(20), sal


numeric(5), desg varchar(8), deptno numeric(3), comm numeric(5),
jdt datetime, hdt datetime);
create table depart1(deptno numeric(3) primary key not null ,
dname varchar(20),loc varchar(8));
create table sss (eno numeric(3) primary key, ename varchar(20),
deptno numeric(3) foreign Key references depart1(Deptno))
alter table employ alter column desg varchar (8)
delete employ
insert into employ values (101,'anil',4500,'sm',10,390,'12-01-
2005','10-05-2006')
insert into employ values (102,'bhanu',5000,'sm',10,350,'01-02-
2004','01-02-2006')
insert into employ values (103,'charan',30000,'md',100,0,'02-05-
2004','02-05-2007')
insert into employ values (104,'danu',15000,'hr',40,null,'03-02-
2004','03-02-2005')
insert into employ values (105,'eswar',8000,'clerk',20,null,'01-
02-2002','01-02-2005')
insert into employ values (105,'eswar1',8888,'clerk',20,null,'02-
02-2002','02-02-2003')
insert into employ values (104,'danu1',11000,'hr',40,null,'03-02-
2004','03-02-2005')
insert into employ values (106,'abhilas',5500,'sm',10,400,'01-02-
2004','01-02-2006')
select * from employ
select * from employ where sal=(select max(sal) from employ) or
sal=(select min(sal) from employ)
select max(sal) from employ where sal<(select max(sal) from
employ)
'select sal from employ e where 2 =(select distinct(sal) from
employ a where e.sal<a.sal)'

alter table employ drop column deptno


select * from employ
alter table employ add deptno varchar(10) foreign key

alter table depart alter column loc varchar(15)


insert into depart values (10,'sales','salesman')
select * from depart
insert into depart values (100,'Manager','manage')
insert into depart values (40,'Hresource','human resource')
insert into depart values (20,'office','officemaintain')

delete from employ where eno='105'


select * from employ
select * from employ where desg in ('sm','hr')

select the nth maximum sal


'second minimum salary'

select * FROM EMPLOY A WHERE 2 =(SELECT COUNT(DISTINCT(SAL))


FROM EMPLOY B WHERE A.SAL >= B.SAL)

select top 3 * from employ order by sal asc


'selecting the duplicate records'

select * from employ where eno in(select eno from employ group
by eno having count(eno)>1) order by eno

select * from employ where eno in(select eno from employ group
by eno having count(eno)>1) order by eno
select * from employ where ename like 'd%'
select * from employ where ename like '%u'
select * from employ where ename like '_h%'
select * from employ where ename like '_____'
select * from employ -(select * from employ where eno in (select
hr from employ))

select eno,ename,desg from employ where desg not in ('sm','hr')


select ename from employ where len(ename)=5
select count(*) from employ
select sum(sal)+sum(comm) from employ
select min(sal) from employ
select avg(sal) from employ
select min(sal) from employ where desg='sm'
select * from employ where sal=(select max(sal) from employ where
desg='sm')
select avg(sal) from employ where desg='sm'
select * from employ order by sal asc
select * from employ order by sal desc
select * from employ order by ename desc
select * from employ order by deptno,ename,sal
select ename,12*(sal+comm) annual from employ order by
12*(sal+comm) desc
select * from employ e where 0=(select count(sal) from employ
where e.sal>sal)
select ename, sal,sal*0.2 'HRA', sal*0.15 'DA',sal*0.05 'PF',sal+
(sal*0.2)+(sal*0.15)-(sal*0.05) 'Net Salary' from employ
select deptno,count(*) from employ group by deptno
select deptno, sum(sal)+sum(comm) 'sum' from employ group by
deptno
select deptno,max(sal) 'maximum sal in the
departno',min(sal)'minimam salary in the deptno' from employ
group by deptno
select ename,desg,sal from employ where sal<(select max(sal) from
employ where desg='hr') and sal>(select min(sal) from employ
where desg='clerk')
select * from employ e where sal=(select max(sal) from employ
where deptno=e.deptno)
select * from employ e where sal in (select max(sal) from employ
group by desg having e.desg=desg)
set pause
select * from employ where jdt between '01-02-2004' and '01-02-
2006'
select * from employ order by sal
select desg from employ group by desg

select getdate()

select * from employ where eno not in( select desghr from
employ)
select * from employ
select * from emp
create table emp (empno integer,ename char(10),sal
numeric(10,2),mgr integer, jdate datetime)
insert into emp values (200,'Mahesh',9000,100,'10-aug-2005')
select * from emp where mgr is not null and empno not in
(select mgr from emp where mgr is null )
select * from emp where empno not in (select mgr from emp where
mgr is not null )
select top 5 sal from emp order by sal desc
select a.sal from emp a where sal>(select e.sal from emp e where
e.sal>a.sal)
select * from emp where empno in (select empno,count(empno) from
emp group by empno having count(empno)>1)
select empno,count(distinct(empno)) from emp group by empno
having count(empno)>1
select e.ename ,m.ename from emp e, emp m where e.mgr=m.empno
SELECT empno, COUNT(EMPNO) FROM EMP GROUP BY EMPNO
select ename from employ where sal=(select max(sal) from employ)
select ename,desg from employ where sal=(select max(sal) from
employ where desg='sm')
update employ set sal=12000 where eno=106
update employ set sal=11000 where ename='danu'
select * from employ where desg='sm' and sal>(select max(sal)
from employ where desg='clerk')
select * from employ where sal=(select max(sal) from employ where
desg='sm')
select mgr from emp where mgr is not null
select count(*) from emp
select distinct(sal) from emp where sal =(select
max(distinct(sal)) from emp) or sal=(select min(distinct(sal))
from emp)
select ename from employ where deptno=(select distinct(deptno)
from employ where desg='sm')
select ename from employ where deptno in(select deptno from
employ where desg='sm')
select desg,sum(sal+isnull(comm,0))from employ group by desg
having sum(sal+isnull(comm,0))>(select max(sal) from employ where
desg='hr')
select datediff(year,jdt,getdate()) from employ
select * from employ where comm is null or comm>0
select * from employ where datediff (year,jdt,hdt)>=3
SELECT ENO, DATEDIFF (MONTH,JDT,HDT) FROM EMPLOY
SELECT * FROM EMPLOY WHERE DATEDIFF (MONTH,JDT,HDT) > 12
SELECT * FROM EMPLOY WHERE DESG IN('SM','HR','MD') AND SAL>=5000
SELECT GETDATE()
SELECT USER_NAME()
SELECT name
FROM sysusers
WHERE name = USER_NAME()
GO
SELECT 'The current user is: '+ convert(char(30), CURRENT_USER)
GO
SELECT 'The current user is: '+ CURRENT_USER
SELECT * FROM EMPLOY WHERE ENAME LIKE B
SHOW USER

alter table alter column depart1 drop foreign key


select * from employ
insert into
select * from employ e where 2=(select count(sal) from employ f
where e.sal>f.sal) order by sal desc

select desg,sum(sal+ comm )'Total' from employ group by desg


having sum(comm)is not null
select desg,sum(sal+isnull (comm,0)) from employ group by desg
having sum(sal+isnull(comm,0))>10000
select desg, count(*) from employ group by desg having count(*)>2
select eno,ename,desg,sal=12000 from employ where eno=106

'ISNULL(price, 0.00) AS Price'

select desg, sum (sal) from employ group by desg having


sum(sal)>20000
select deptno,count(*) from employ group by deptno having
count(*)>2
select deptno, max(sal) from employ group by deptno

Question:  How can I retrieve the Top N records from a query?

For example, what if I wanted to retrieve the first 3 records from my query results. How can I do this?

Answer:  To retrieve the Top N records from a query, you can use the following syntax:
SELECT *
FROM (your ordered query) alias_name
WHERE rownum <= Rows_to_return
ORDER BY rownum;

For example, if you wanted to retrieve the first 3 records from the suppliers table, sorted by supplier_name in
ascending order, you would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_name) suppliers2
WHERE rownum <= 3
ORDER BY rownum;

If you wanted to retrieve the first 3 records from the suppliers table, sorted by supplier_name in descending order,
you would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_name DESC) suppliers2
WHERE rownum <= 3
ORDER BY rownum;

If you wanted to retrieve the first 5 records from the suppliers table, sorted by supplier_id in ascending order, you
would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_id) suppliers2
WHERE rownum <= 5
ORDER BY rownum;

If you wanted to retrieve the first 5 records from the suppliers table, sorted by supplier_id in descending order, you
would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_id DESC) suppliers2
WHERE rownum <= 5
ORDER BY rownum;

Oracle/PLSQL: Retrieve Bottom N records from a query

Question:  How can I retrieve the Bottom N records from a query?

For example, what if I wanted to retrieve the last 3 records from my query results. How can I do this?
Answer:  To retrieve the Bottom N records from a query, you can use the following syntax:

SELECT *
FROM (your query ordered in reverse) alias_name
WHERE rownum <= Rows_to_return
ORDER BY rownum DESC;

If you want to retrieve the bottom records from a query, you need to order your results in reverse in the "your query
ordered in reverse" section of the solution above.

For example, if you wanted to retrieve the last 3 records from the suppliers table, sorted by supplier_name in
ascending order, you would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_name DESC) suppliers2
WHERE rownum <= 3
ORDER BY rownum DESC;

Notice that although you want the last 3 records sorted by supplier_name in ascending order, you actually sort the
supplier_name in descending order in this solution.

If you wanted to retrieve the last 3 records from the suppliers table, sorted by supplier_name in descending order,
you would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_name) suppliers2
WHERE rownum <= 3
ORDER BY rownum DESC;

If you wanted to retrieve the last 5 records from the suppliers table, sorted by supplier_id in ascending order, you
would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_id DESC) suppliers2
WHERE rownum <= 5
ORDER BY rownum DESC;

If you wanted to retrieve the last 5 records from the suppliers table, sorted by supplier_id in descending order, you
would run the following query:

SELECT *
FROM (select * from suppliers ORDER BY supplier_id) suppliers2
WHERE rownum <= 5
Oracle/PLSQL: Retrieve Middle N records from a query

Question:  How can I retrieve the Middle N records from a query?

For example, what if I wanted to retrieve records 3 to 5 from my query results. How can I do this?

Answer:  To retrieve the Middle N records from a query, you can use the following syntax:

SELECT *
FROM (select alias_name.*, rownum rnum from
(-- your ordered query --) alias_name
where rownum <= UPPER_BOUND )
WHERE rnum >= LOWER_BOUND;

For example, if you wanted to retrieve records 3 through 5 from the suppliers table, sorted by
supplier_name in ascending order, you would run the following query:

SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_name) suppliers2
where rownum <= 5 )
WHERE rnum >= 3;

If you wanted to retrieve records 3 through 5 from the suppliers table, sorted by supplier_name
in descending order, you would run the following query:

SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_name DESC) suppliers2
where rownum <= 5 )
WHERE rnum >= 3;

If you wanted to retrieve records 2 through 4 from the suppliers table, sorted by supplier_id in
ascending order, you would run the following query:

SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_id) suppliers2
where rownum <= 4 )
WHERE rnum >= 2;

If you wanted to retrieve records 2 through 4 from the suppliers table, sorted by supplier_id in
descending order, you would run the following query:

SELECT *
FROM (select suppliers2.*, rownum rnum from
(select * from suppliers ORDER BY supplier_id DESC) suppliers2
where rownum <= 4 )
WHERE rnum >= 2;

\Oracle/PLSQL: Retrieve second highest value from a table

Question:  How can I retrieve the second highest salary amount from a salary table?

Answer:  To retrieve the second highest salary from a salary table, you could run the following query: (please note
that the subquery is sorted in descending order)

SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 2 )
WHERE rnum >= 2;

If you wanted to retrieve all fields from the salary table for the second highest salary, you could run the following
query: (please note that the subquery is sorted in descending order)

SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 2 )
WHERE rnum >= 2;

Oracle/PLSQL: Retrieve third highest value from a table

Question:  How can I retrieve the third highest salary amount from a salary table?

Answer:  To retrieve the third highest salary from a salary table, you could run the following query: (please note that
the subquery is sorted in descending order)

SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 3 )
WHERE rnum >= 3;

If you wanted to retrieve all fields from the salary table for the third highest salary, you could run the following query:
(please note that the subquery is sorted in descending order)
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 3 )
WHERE rnum >= 3;

Question:  How can I retrieve the second lowest salary amount from a salary table?

Answer:  To retrieve the second lowest salary from a salary table, you could run the following query: (please note
that the subquery is sorted in ascending order)

SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 2 )
WHERE rnum >= 2;

If you wanted to retrieve all fields from the salary table for the second lowest salary, you could run the following
query: (please note that the subquery is sorted in ascending order)

SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 2 )
WHERE rnum >= 2;

Question:  How can I retrieve the third lowest salary amount from a salary table?

Answer:  To retrieve the third lowest salary from a salary table, you could run the following query: (please note that
the subquery is sorted in ascending order)

SELECT salary_amount
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 3 )
WHERE rnum >= 3;

If you wanted to retrieve all fields from the salary table for the third lowest salary, you could run the following query:
(please note that the subquery is sorted in ascending order)

SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount) salary2
where rownum <= 3 )
WHERE rnum >= 3;
Question:  How do I execute an SQL script file in SQLPlus?

Answer:  To execute a script file in SQLPlus, type @ and then the file name.

SQL >  @{file}

For example, if your file was called script.sql, you'd type the following command at the SQL prompt:

SQL >  @script.sql

The above command assumes that the file is in the current directory. (ie: the current directory is usually the directory
that you were located in before you launched SQLPlus.)

If you need to execute a script file that is not in the current directory, you would type:

SQL >  @{path}{file}

For example:

SQL >  @/oracle/scripts/script.sql

Question:  I have a date field in an Oracle table and I want to insert a new record. I'm trying to insert a date with a
time component into this field, but I'm having some problems.

How can I insert this value into the table.

For example, the value is '3-may-03 21:02:44'

Answer:  To insert a date/time value into the Oracle table, you'll need to use the to_date function. The to_date
function allows you to define the format of the date/time value.

For example, we could insert the '3-may-03 21:02:44' value as follows:

insert into table_name


(date_field)
values
(to_date('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'));

Oracle/PLSQL: To_Date Function


In Oracle/PLSQL, the to_date function converts a string to a date.

The syntax for the to_date function is:

to_date( string1, [ format_mask ], [ nls_language ] )

string1 is the string that will be converted to a date.

format_mask is optional. This is the format that will be used to convert string1 to a date.

nls_language is optional. This is the nls language used to convert string1 to a date.

The following is a list of options for the format_mask parameter. These parameters can be used in many
combinations.

Parameter Explanation
YEAR Year, spelled out
YYYY 4-digit year
YYY
YY Last 3, 2, or 1 digit(s) of year.
Y
IYY
IY Last 3, 2, or 1 digit(s) of ISO year.
I
IYYY 4-digit year based on the ISO standard
Accepts a 2-digit year and returns a 4-digit year.
RRRR A value between 0-49 will return a 20xx year.
A value between 50-99 will return a 19xx year.
Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).
MM Month (01-12; JAN = 01).
MON Abbreviated name of month.
MONTH Name of month, padded with blanks to length of 9 characters.
RM Roman numeral month (I-XII; JAN = I).
Week of year (1-53) where week 1 starts on the first day of the year
WW
and continues to the seventh day of the year.
Week of month (1-5) where week 1 starts on the first day of the
W
month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.
D Day of week (1-7).
DAY Name of day.
DD Day of month (1-31).
DDD Day of year (1-366).
DY Abbreviated name of day.
J Julian day; the number of days since January 1, 4712 BC.
HH Hour of day (1-12).
HH12 Hour of day (1-12).
HH24 Hour of day (0-23).
MI Minute (0-59).
SS Second (0-59).
SSSSS Seconds past midnight (0-86399).
Fractional seconds. Use a value from 1 to 9 after FF to indicate the
FF
number of digits in the fractional seconds. For example, 'FF4'.
AM, A.M., PM, or P.M. Meridian indicator
AD or A.D AD indicator
BC or B.C. BC indicator
TZD Daylight savings information. For example, 'PST'
TZH Time zone hour.
TZM Time zone minute.
TZR Time zone region.

For example:

to_date('2003/07/09', 'yyyy/mm/dd') would return a date value of July 9, 2003.


to_date('070903', 'MMDDYY') would return a date value of July 9, 2003.
to_date('20020315', 'yyyymmdd') would return a date value of Mar 15, 2002.

Anda mungkin juga menyukai