Sie sind auf Seite 1von 18

QUERIES

1. Display the dept information from department table.


select * from dept;
2. Display the details of all employees.
select * from emp;
3. Display the name and job for all employees.
select ename, job from emp;
4. Display name and salary for all employees.
select ename, sal from emp;
5. Display employee number and total salary for each employee.
select empno, sal+nvl(comm,0) from emp;
6. Display employee name and annual salary for all employees.
select empno, empname, 12*sal+nvl(comm,0) annualsal from emp;
7. Display the names of all employees who are workin in department number 1!.
select ename from emp where deptno=10;
". Display the names of all employees workin as clerks and drawin a salary
more than 3!!!.
select ename from emp where job=!"#$ and sal%&000;
#. Display employee number and names for employees who earn commission.
select empno, ename from emp where comm 's not null and comm%0;
1!. Display names of employees who do not earn any commission.
(elect empno, ename from emp where comm 's null or comm=0;
11. Display the names of employees who are workin as clerk$ salesman or analyst
and drawin a salary more than 3!!!.
select ename from emp where (job=!"#$ or job=()!"(*)+ or
job=)+)!,(-) and sal%&000;
%or&
select ename from emp where job 'n(.!"#$,()!"(*)+,)+)!,(-)
and sal%&000;
12. Display the names of employees who are workin in the company for the past
5 years.
select ename from emp where s/sdate0h'redate%1*&21;
13. Display the list of employees who ha'e joined the company before 3!
th
(une #!
or after 31
st
dec #!.
select * from emp where h'redate between .&00jun01330 and .&10dec0
1330;
14. Display current date.
select s/sdate from dual;
244549367.doc Page 1 of 18
QUERIES
15. Display the list of users in your database %usin lo table&.
select * from dba4users;
16. Display the names of all tables from the current user.
select * from tab;
17. Display the name of the current user.
show user; or select user from dual;
1". Display the names of employees workin in department number 1! or 2! or 4!
or employees workin as clerks$ salesman or analyst.
select ename from emp where deptno 'n (10,20,50) or job 'n
(.!"#$,()!"(*)+,)+)!,(-);
1#. Display the names of employees whose name starts with alphabet ).
select ename from emp where ename l'6e .(7;
2!. Display employee names for employees whose name ends with alphabet.
select ename from emp where ename l'6e .7(;
21. Display the names of employees whose names ha'e second alphabet * in their
names.
select ename from emp where ename l'6e .4)7;
22. Display the names of employees whose name is e+actly fi'e characters in
lenth.
select ename from emp where len8th(ename)=1;
%or&
select ename from emp where ename l'6e 9444449;
23. Display the names of employees who are not workin as manaers.
select * from emp m'nus (select * from emp where empno 'n (select
m8r from emp));
%or&
select * from emp where empno not 'n (select m8r from emp where
m8r 's not null);
%or&
select * from emp e where empno not 'n (select m8r from emp where
e:empno=m8r)
24. Display the names of employees who are not workin as )*,-).*/ or 0,-12
or */*,3)4.
select ename from emp where job not 'n
(.!"#$,()!"(*)+,)+)!,(-);
25. Display all rows from -.5 table. 4he system should wait after e'ery screen full
of information.
set pause on;
26. Display the total number of employees workin in the company.
select count(*) from emp;
27. Display the total salary bein paid to all employees.
244549367.doc Page 2 of 18
QUERIES
select sum(sal)+sum(nvl(comm,0)) from emp;
2". Display the ma+imum salary from emp table.
select ma;(sal) from emp;
2#. Display the minimum salary from emp table.
select m'n(sal) from emp;
3!. Display the a'erae salary from emp table.
select av8(sal) from emp;
31. Display the ma+imum salary bein paid to 0,-12.
select ma;(sal) from emp where job=!"#$;
32. Display the ma+imum salary bein paid in dept no 2!.
select ma;(sal) from emp where deptno=20;
33. Display the min )al bein paid to any )*,-).*/.
select m'n(sal) from emp where job=()!"(*)+;
34. Display the a'erae salary drawn by manaers.
select av8(sal) from emp where job=*)+)<"#;
35. Display the total salary drawn by analyst workin in dept no 4!.
select sum(sal)+sum(nvl(comm,0)) from emp where deptno=50;
36. Display the names of employees in order of salary i.e. the name of the
employee earnin lowest salary should appear first.
select ename from emp order b/ sal;
37. Display the names of employees in descendin order of salary.
select ename from emp order b/ sal desc;
3". Display the details from emp table in order of emp name.
select ename from emp order b/ ename;
3#. Display empno$ ename$ deptno$ and sal. )ort the output first based on name
and within name by deptno and within deptno by )al6
select * from emp order b/ ename,deptno,sal;
4!. Display the name of the employee alon with their annual salary %)al 7 12&.
4he name of the employee earnin hihest annual salary should appear first.
select ename, 12*(sal+nvl(comm,0)) )nnual from emp order b/
12*(sal+nvl(comm,0)) desc;
41. Display name$ )al$ hra$ pf$ da$ total )al for each employee. 4he output should
be in the order of total )al$ hra 158 of )al$ da 1!8 of sal$ pf 58 of sal total
salary will be %sal9hra9da&:pf.
select ename,sal,sal*11=100 >#), sal*1=100 ?@, sal*10=100
A),sal+sal*11=1000sal*1=100+sal*10=100 -B-)!4()!)#, from emp
244549367.doc Page 3 of 18
QUERIES
42. Display dept numbers and total number of employees within each roup.
select deptno,count(*) from emp 8roup b/ deptno;
43. Display the 'arious jobs and total number of employees with each job roup.
select job, count(*) from emp 8roup b/ job;
44. Display department numbers and total salary for each department.
select deptno, sum(sal) from emp 8roup b/ deptno;
45. Display department numbers and ma+imum salary for each department.
select deptno, ma;(sal),m'n(sal) from emp 8roup b/ deptno;
46. Display the 'arious jobs and total salary for each job.
select job, sum(sal) from emp 8roup b/ job;
47. Display each job alon with minimum sal bein paid in each job roup.
select job, m'n(sal) from emp 8roup b/ job;
4". Display the department numbers with more than three employees in each dept.
select deptno, count(*) from emp 8roup b/ deptno hav'n8 count(*)%&;
4#. Display the 'arious jobs alon with total sal for each of the jobs where total sal
is reater than 4!!!!.
select job, sum(sal) from emp 8roup b/ job hav'n8 sum(sal)%50000;
5!. Display the 'arious jobs alon with total number of employees in each job. 4he
output should contain only those jobs with more than three employees.
select job, count(*) from emp 8roup b/ job hav'n8 count(*)%&;
51. Display the name of emp who earns hihest sal.
select ename from emp where sal=(select ma;(sal) from emp);
52. Display the employee number and name of employee workin as 0,-12 and
earnin hihest salary amon 0,-12).
select empno, ename from emp where job=9!"#$9 and sal=(select
ma;(sal) from emp where job=9!"#$9);
53. Display the names of the salesman who earns a salary more than the hihest
salary of any clerk.
select ename from emp where job=()!"(*)+ and sal %
(select ma;(sal) from emp where job=9!"#$9);
54. Display the names of clerks who earn salary more than that of (ames of that of
sal lesser than that of )cott.
select ename from emp where job=9!"#$9 and salC(select sal from
emp where ename=9(B--9) and sal%(select sal from emp where
ename=9D)*"(9);
55. Display the names of employees who earn a )al more than that of (ames or
that of salary reater than that of )cott.
244549367.doc Page 4 of 18
QUERIES
select ename from emp where sal %
(select sal from emp where ename=9(B--9) and sal %
(select sal from emp where ename=9D)*"(9);
56. Display the names of the employees who earn hihest salary in their respecti'e
departments.
select * from emp e where sal =
(select ma;(sal) from emp where deptno=e:deptno)
57. Display the names of employees who earn hihest salaries in their respecti'e
job roups.
select * from emp e where sal 'n
(select ma;(sal) from emp 8roup b/ job hav'n8 e:job=job)
5". Display the employee names who are workin in accountins dept.
select ename from emp where deptno =
(select deptno from dept where dname=E)BF+-G+<E);
%or&
select ename from emp where deptno 'n (select deptno from dept
where dname=E)BF+-G+<E);
5#. Display the employee names who are workin in 0hicao.
select ename from emp where deptno =
(select deptno from dept where loc=>G)<B);
6!. Display the job roups ha'in total salary reater then the ma+imum salary for
manaers.
select job, sum(sal) from emp 8roup b/ job hav'n8 sum(sal) %
(select ma;(sal) from emp where job=9*)+)<"#9);
61. Display the names of employees from department number 1! with salary
reater than that of any employee workin in other departments.
select ename,sal,deptno from emp e where deptno=10 and sal %
an/(select sal from emp where e:deptnoH=deptno);
62. Display the names of employee from department number 1! with salary
reater then that of all employee workin in other departments.
select ename, sal, deptno from emp e where deptno=10 and sal %
an/(select sal from emp where e:deptno H= deptno);
63. Display the names of employees in ;pper case.
select upper(ename) from emp;
64. Display the names of employees in lower case.
select lower(ename) from emp;
65. Display the name of employees in proper case.
select 'n'tcap(ename) from emp;
66. <ind out the lenth of your name usin appropriate function.
select len8th(.Gnd'a) from dual;
244549367.doc Page 5 of 18
QUERIES
67. Display the lenth of all employees= names.
select sum(len8th(ename)) from emp;
6". Display the name of the employee concatenate with -.5 no.
select enameIIempno from emp;
%or&
select concat(ename,empno) from emp;
6#. ;se appropriate function and e+tract 3 characters startin from 2 characters
from the followin strin >?racle= i.e. the output should be >rac=.
select substr(.oracle,2,&) from dual;
7!. <ind the first occurrence of character a from the followin strin >computer
maintenance corporation=.
select 'nstr(.computer ma'ntenance corporat'on,a,1,1) from dual;
71. 1eplace e'ery occurrence of alphabet * with @ in the strin *llen=s %user
translate function&.
select replace(9)llens9,9)9,9b9) from dual;
72. Display the information from -.5 table. Ahere'er job >manaer= is found it
should be displayed as boss%replace function&.
select empno, ename, replace(job, 9*)+)<"#9, 9Joss9) DBJ from emp;
73. Display empno$ ename$ deptno from -.5 table. Bnstead of display department
numbers display the related department name %use decode function&.
select e:empno, e:ename, d:dname from emp e,dept d where
e:deptno = d:deptno;
74. Display your ae in days.
select round(s/sdate0to4date(9110au80135K9)) from dual;
75. Display your ae in months.
select floor(months4between(s/sdate,9110au80135K9))
La8e 'n monthsL from dual;
76. Display current date as 15
th
auust <riday nineteen forty se'en.
select to4char(s/sdate,9ddth month da/ /ear9) from dual;
77. Display the followin output for each row from -.5 table as >scott has joined
the company on Aednesday 13
th
auust nineteen ninety=.
select enameII9 has jo'ned the compan/ on 9IIto4char(h'redate,9da/
ddth month /ear9) from emp;
7". <ind the date of nearest )aturday after current day.
select ne;t4da/(s/sdate, 9()-F#A),9) from dual;
7#. Display current time.
select to4char(s/sdate,9hhMm'Mss9) -'me from dual;
"!. Display the date three months before the current date.
select add4months(s/sdate,0&) from dual;
244549367.doc Page 6 of 18
QUERIES
"1. Display the common jobs from department number 1! and 2!.
select job from emp where deptno=10 and job 'n(select job from emp
where deptno=20);
%or&
select job from emp where deptno=10 'ntersect select job from emp
where deptno=20;
"2. Display the jobs found in department number 1! and 2! eliminate duplicate
jobs.
select d'st'nct(job) from emp where deptno=10 and job 'n(select job
from emp where deptno=20);
%or&
select job from emp where deptno=10 'ntersect select job from emp
where deptno=20;
"3. Display the jobs which are uniCue to dept no 1!.
select job from emp where deptno=10 m'nus select job from emp
where deptnoH=10;
%or&
select job from emp where deptno = 10 and job not 'n (select job from
emp where deptnoC%10);
"4. Display the details of those who do not ha'e any person workin under them.
select empno from emp where empno not 'n (select m8r from emp
where m8r 's not null);
"5. Display the details of employees who are in sales dept and rade is 3.
select * from emp where sal%=(select losal from sal8rade where
8rade=&) and salC=(select h'sal from sal8rade where 8rade=&) and
deptno=(select deptno from dept where dname=9()!"(9);
"6. Display those who are not manaers and who are manaers any one.
select * from emp where empno 'n(select m8r from emp) un'on
select * from emp where empno not 'n(select m8r from emp where
m8r 's not null);
"7. Display those employees whose name contains not less than 4 chars.
(elect * from emp where len8th(ename)%5;
"". Display those departments whose name start with >)= while location name end
with >?=.
select * from dept where dname l'6e 9(79 and loc l'6e 97B9;
"#. Display those employees whose manaer name is (?/-).
select * from emp where m8r=(select empno from emp where
ename=9DB+"(9);
#!. Display those employees whose salary is more than 3!!! after i'in 2!8
increment.
select * from emp where sal*120=100 % &000;
244549367.doc Page 7 of 18
QUERIES
%or&
select * from emp where sal+sal*20=100 % &000;
#1. Display all employees with there dept name.
select ename, dname from emp e, dept d where e:deptno = d:deptno;
#2. Display ename who are workin in sales dept.
select empno, ename from emp where
deptno=(select deptno from dept where dname=9()!"(9);
#3. Display employee name$ deptname$ salary and comm. for those )al in between
2!!! to 5!!! while location is 0hicao.
select empno,ename,deptno from emp where deptno=(select deptno
from dept where loc=9>G)<B9) and sal between 2000 and 1000;
#4. Display those employees whose salary reater than his manaer salary.
select * from emp e where sal%(select sal from emp where
empno=e:m8r);
#5. Display those employees who are workin in the same dept where his manaer
is workin.
select * from emp e where
deptno = (select deptno from emp where empno=e:m8r);
#6. Display those employees who are not workin under any maner.
select * from emp where m8r 's null or empno=m8r;
#7. Display rade and employees name for the dept no 1! or 3! but rade is not 4$
while joined the company before 31:dec:"2.
select empno,ename,sal,deptno,h'redate,8rade from emp e,sal8rade s
where e:sal%=s:losal and e:salC=s:h'sal and deptno 'n(10,&0) and
8radeC%5 and h'redateC9010dec013N19;
#". ;pdate the salary of each employee by 1!8 increments that are not eliible for
commission.
update emp set sal=sal+(sal*10=100) where comm 's null;
##. Delete those employees who joined the company before 31:dec:"2 while there
dept location is >/-A 3?12= or >0DB0*E?=.
delete from emp where h'redateC9&10dec013N29 and deptno 'n
(select deptno from dept where loc 'n(9+"O ,B#$9,9>G)<B9));
1!!. Display employee name$ job$ deptname$ location for all who are workin as
manaers.
select ename,job,dname,loc from emp e, dept d where
e:deptno=d:deptno and empno 'n (select m8r from emp);
1!1. Display those employees whose manaer names is (ones$ and also display
there manaer name.
select e:empno, e:ename, m:ename *)+)<"# from emp e, emp m
where e:m8r=m:empno and m:ename=9DB+"(9;
244549367.doc Page 8 of 18
QUERIES
1!2. Display name and salary of ford if his )al is eCual to hih )al of his rade.
select ename,sal from emp e where ename=9@B#A9 and sal=(select
h'sal from sal8rade where 8rade=(select 8rade from sal8rade where
e:sal%=losal and e:salC=h'sal));
1!3. Display employee name$ his job$ his dept name$ his manaer name$ his rade
and make out of an under department wise.
brea6 on deptno;
select d:deptno, e:ename, e:job, d:dname, m:ename, s:8rade from
emp e, emp m, dept d, sal8rade s where e:deptno=d:deptno and e:sal
between s:losal and s:h'sal and e:m8r=m:empno order b/ e:deptno;
1!4. ,ist out all the employees name$ job$ and salary rade and department name
for e'ery one in the company e+cept >0,-12=. )ort on salary display the hihest
salary.
select empno, ename, sal, dname, 8rade from emp e, dept d, sal8rade s
where e:deptno=d:deptno and e:sal between s:losal and s:h'sal and
e:jobC%9!"#$9 order b/ sal;

1!5. Display employee name$ his job and his manaer. Display also employees who
are without manaer.
select e:ename, e:job, m:ename *ana8er from emp e,emp m where
e:m8r=m:empno un'on select ename,job,9no mana8er9 from emp where
m8r 's null;
1!6. <ind out the top 5 earner of company.
select * from emp e where 1%(select count(*) from emp where
sal%e:sal) order b/ sal desc;
1!7. Display the name of those employees who are ettin hihest salary.
select empno,ename,sal from emp where sal=(select ma;(sal) from
emp);
1!". Display those employees whose salary is eCual to a'erae of ma+imum and
minimum.
select * from emp where sal=(select (ma;(sal)+m'n(sal))=2 from
emp);
1!#. Display count of employees in each department where count reater than 3.
select deptno, count(*) from emp 8roup b/ deptno hav'n8 count(*)%&;
11!. Display dname where at least 3 are workin and display only dname.
select dname from dept where deptno 'n
(select deptno from emp 8roup b/ deptno hav'n8 count(*)%&);
111. Display name of those manaers name whose salary is more than a'erae
salary of company.
select ename, sal from emp where empno 'n(select m8r from emp) and
sal % (select av8(sal) from emp);
244549367.doc Page 9 of 18
QUERIES
112. Display those manaers name whose salary is more than an a'erae salary of
his employees.
select ename, sal from emp e where empno 'n(select m8r from emp)
and e:sal%(select av8(sal) from emp where m8r=e:empno);
113. Display employee name$ )al$ comm and net pay for those employees whose
net pay are reater than or eCual to any other employee salary of the
companyF
select ename, sal, comm, sal+nvl(comm,0) net?a/ from emp where
sal+nvl(comm:,0)%=an/(select sal from emp);
114. Display those employees whose salary is less than his manaer but more than
salary of any other manaers.
select * from emp e where salC(select sal from emp where empno =
e:m8r) and sal%an/(select sal from emp where empnoH=e:m8r);
115. Display all employees names with total )al of company with employee name.
)elect ename$
116. <ind out the last 5%least& earner of the companyF
select * from emp e where 1%(select count(*) from emp where
salCe:sal) order b/ sal;
117. <ind out the number of employees whose salary is reater than there manaer
salaryF
select count(*) from emp e where sal%(select sal from emp where
empno=e:m8r);
11". Display those manaer who are not workin under president but they are
workin under any other manaerF
select * from emp e where m8r 'n(select empno from emp where
enameC%9$G+<9);
11#. Delete those department where no employee workinF
delete from dept d where 0=(select count(*) from emp where
deptno=d:deptno);
12!. Delete those records from -.5 table whose deptno not a'ailable in dept tableF
delete from emp where deptno not 'n(select deptno from dept);
121. Display those earners whose salary is out of the rade a'ailable in )al rade
tableF
select * from emp where salC(select m'n(losal) from sal8rade) or
sal%(select ma;(h'sal) from sal8rade);
122. Display employee name$ )al$ comm. and whose net pay is reater than any
other in the companyF
(elect ename, sal, comm from emp where sal+sal*11=1000sal*1=100
+sal*10=100 = (select ma;(sal+sal*11=1000sal*1=100+sal*10=100)
from emp);
244549367.doc Page 10 of 18
QUERIES
123. Display name of those employees who are oin to retire 31:dec:##. Bf the
ma+imum job is period is 1" yearsF
select * from emp where (to4date(9&10dec013339)0h'redate)=&21%1N;
124. Display those employees whose salary is ?DD 'alueF
select * from emp where mod(sal,2)=1;
125. Display those employees whose salary contains at least 4 diitsF
select * from emp where len8th(sal)%=5;
126. Display those employees who joined in the company in the month of D-0F
select * from emp where upper(to4char(h'redate,9mon9))=9A"9;
127. Display those employees whose name contains G*HF
select * from emp where 'nstr(ename,9)9,1,1)%0;
12". Display those employees whose deptno is a'ailable in salaryF
select * from emp where 'nstr(sal,deptno,1,1)%0;
12#. Display those employees whose first 2 characters from hire date:last 2
characters of salaryF
select substr(h'redate,0,2)IIsubstr(sal,len8th(sal)01,2) from emp;
select concat( substr(h'redate,0,2), substr(sal,len8th(sal)01,2) ) from
emp;
13!. Display those employees whose 1!8 of salary is eCual to the year of joininF
select * from emp where to4char(h'redate,9//9)=sal*10=100;
131. Display those employees who are workin in sales or researchF
select * from emp where deptno 'n(select deptno from dept where
dname 'n(9()!"(9,9#"(")#>9));
132. Display the rade of (onesF
select 8rade from sal8rade where losalC=(select(sal) from emp where
ename=9DB+"(9) and h'sal%=(select(sal) from emp where
ename=9DB+"(9);
133. Display those employees who joined the company before 15
th
of the monthF
select empno,ename from emp where h'redateC(to4date(91109II
to4char(h'redate,9mon9)II909IIto4char(h'redate,9////9)));
134. Delete those records where no of employee in a particular department is less
than 3F
delete from emp where deptno 'n(select deptno from emp 8roup b/
deptno hav'n8 count(*)%&);
135. Delete those employees who joined the company 21 years back from todayF
select * from emp where round((s/sdate0h'redate)=&21)%21; or
select * from emp where (to4char (s/sdate, 9////9)0to4char
(h'redate ,9////9) )%21;
244549367.doc Page 11 of 18
QUERIES
136. Display the department name the no of characters of which is eCual to no of
employees in any other departmentF
(elect dname from dept where len8th(dname) 'n (select count(*) from
emp 8roup b/ deptno);
137. Display those employees who are workin as manaerF
select * from emp where empno 'n(select m8r from emp);
13". 0ount the no of employees who are workin as manaer %use set operation&F
select count(*) from emp where empno 'n(select m8r from emp);
13#. Display the name of then dept those employees who joined the company on
the same dateF
select empno,ename,h'redate,deptno from emp e where h'redate 'n
(select h'redate from emp where empnoC%e:empno);
14!. Display those employees whose rade is eCual to any number of )al but not
eCual to first number of )alF
141. Display the manaer who is ha'in ma+imum number of employees workin
under himF
(elect m8r from emp 8roup b/ m8r hav'n8 count(*)=(select
ma;(count(m8r)) from emp 8roup b/ m8r);
142. ,ist out employees name and salary increased by 158 and e+pressed as whole
number of dollarsF
select empno,ename,lpad(concat(9P9,round(sal*111=100)),K) salar/
from emp;
143. 5roduce the output of the -.5 table G-.5,?3--I*/DI(?@H for ename and jobF
select * from "*?!B,""4)+A4DBJ;
144. ,ist all employees with hire date in the format >(une 4 1#""=F
select to4char(h'redate,9month dd ////9) from emp;
145. 5rint a list of employees displayin >,ess )alary= if less than 15!! if e+actly
15!! display as >-+act )alary= and if reater than 15!! display >.ore )alary=F
select empno,ename,9!ess (alar/ 9IIsal from emp where salC1100
un'on
select empno,ename,9*ore (alar/ 9IIsal from emp where sal%1100
un'on
select empno,ename,9";act (alar/ 9IIsal from emp where sal=1100
146. Arite Cuery to calculate the lenth of employee has been with the companyF
(elect round(s/sdate0h'redate) from emp;
147. Ei'en a )trin of the format >nnJnn= 'erify that the first and last 2 characters
are numbers. *nd that the middle characters is >y= print the e+pressions >3es= if
'alid >/o= of not 'alid use the followin 'alues to test your solution
244549367.doc Page 12 of 18
QUERIES
14". -mployees hire on 15
th
of any month are paid on the last <riday of that month.
4hose hired after 15
th
are paid the last <riday of the followin month. print a list
of employees their hire date and first pay date sort those whose )al contains
first diits of their dept.
14#. Display those maners who are ettin less than his employees )al.
(elect empno from emp e where salCan/(select sal from emp where
m8r=e:empno);
15!. 5rint the details of all the employees who are sub ordinate to @lake.
(elect * from emp where m8r=(select empno from emp where
ename=9J!)$"9);
151. Display those who workin as manaer usin co related sub Cuery.
(elect * from emp where empno 'n(select m8r from emp);
152. Display those employees whose maner name is (ones and also with his
manaer name.
(elect * from emp where m8r=(select empno from emp where
ename=9DB+"(9) un'on select * from emp where empno=(select m8r
from emp where ename=9DB+"(9);
153. Define 'ariable representin the e+pressions used to calculate on employee=s
total annual renumaration.
def'ne emp4ann4sal=(sal+nvl(comm,0))*12;
154. ;se the 'ariable in a statement which finds all employees who can earn 3!$!!!
a year or more.
select * from emp where Qemp4ann4sal%&0000;
155. <ind out how many maners are there with out listin them.
(elect count (*) from "*? where empno 'n (select m8r from "*?);
156. <ind out the a' sal and a' total remuneration for each job type remember
salesman earn commission.
select job,av8(sal+nvl(comm,0)),sum(sal+nvl(comm,0)) from emp
8roup b/ job;
157. 0heck whether all employees number are indeed uniCue.
select count(empno),count(d'st'nct(empno)) from emp hav'n8
count(empno)=(count(d'st'nct(empno)));
15". ,ist out the lowest paid employees workin for each manaer$ e+clude any
roups where min sal is less than 1!!! sort the output by sal.
select e:ename,e:m8r,e:sal from emp e where sal 'n(select m'n(sal)
from emp where m8r=e:m8r) and e:sal%1000 order b/ sal;
15#. list ename$ job$ annual sal$ deptno$ dname and rade who earn 3!!!! per year
and who are not clerks.
(elect e:ename, e:job, (e:sal+nvl(e:comm,0))*12, e:deptno, d:dname,
s:8rade from emp e, sal8rade s , dept d where e:sal between s:losal
244549367.doc Page 13 of 18
QUERIES
and s:h'sal and e:deptno=d:deptno and (e:sal+nvl(comm,0))*12%
&0000 and e:job C% 9!"#$9;
16!. find out the job that was failed in the first half of 1#"3 and the same job that
was failed durin the same period on 1#"4.
161. find out the all employees who joined the company before their manaer.
(elect * from emp e where h'redateC(select h'redate from emp where
empno=e:m8r);
162. list out the all employees by name and number alon with their manaer=s
name and number also display >/o .anaer= who has no manaer.
select e:empno,e:ename,m:empno *ana8er,m:ename *ana8er+ame
from emp e,emp m where e:m8r=m:empno
un'on
select empno,ename,m8r,9+o *ana8er9 from emp where m8r 's null;
163. find out the employees who earned the hihest )al in each job typed sort in
descendin )al order.
select * from emp e where sal =(select ma;(sal) from emp where
job=e:job);
164. find out the employees who earned the min )al for their job in ascendin
order.
select * from emp e where sal =(select m'n(sal) from emp where
job=e:job) order b/ sal;
165. find out the most recently hired employees in each dept order by hire date
select * from emp order b/ deptno,h'redate desc;
166. display ename$ sal and deptno for each employee who earn a )al reater than
the a' of their department order by deptno
select ename,sal,deptno from emp e where sal%(select av8(sal) from
emp where deptno=e:deptno) order b/ deptno;
167. display the department where there are no employees
select deptno,dname from dept where deptno not 'n(select
d'st'nct(deptno) from emp);
16". display the dept no with hihest annual remuneration bill as compensation.
select deptno,sum(sal) from emp 8roup b/ deptno hav'n8 sum(sal) =
(select ma;(sum(sal)) from emp 8roup b/ deptno);
16#. Bn which year did most people join the company. Display the year and number
of employees
select count(*),to4char(h'redate,9////9) from emp 8roup b/
to4char(h'redate,9////9);
17!. display a' sal fiure for the dept
select deptno,av8(sal) from emp 8roup b/ deptno;
244549367.doc Page 14 of 18
QUERIES
171. Arite a Cuery of display aainst the row of the most recently hired employee.
display ename hire date and column ma+ date showin.
select empno,h'redate from emp where h'redate=(select ma;
(h'redate) from emp);
172. display employees who can earn more than lowest )al in dept no 3!
select * from emp where sal%(select m'n(sal) from emp where
deptno=&0);
173. find employees who can earn more than e'ery employees in dept no 3!
select * from emp where sal%(select ma;(sal) from emp where
deptno=&0);
select * from emp where sal%all(select sal from emp where
deptno=&0);
174. select dept name dept no and sum of )al
brea6 on deptno on dname;
select e:deptno,d:dname,sal from emp e, dept d where
e:deptno=d:deptno order b/ e:deptno;
175. find out a' sal and a' total remainders for each job type
176. find all dept=s which ha'e more than 3 employees
select deptno from emp 8roup b/ deptno hav'n8 count(*)%&;
177. Bf the pay day is ne+t <riday after 15
th
and 3!
th
of e'ery month. Ahat is the
ne+t pay day from their hire date for employee in emp table
17". Bf an employee is taken by you today in your oraniKation. *nd is a policy in
your company to ha'e a re'iew after # months the joined date %and of 1
st
of
ne+t month after # months& how many days from today your employee has to
wait for a re'iew
17#. Display employee name and his sal whose sal is reater than hihest a' of
dept no
1"!. Display the 1!
th
record of -.5 table. %without usin rowid&
1"1. Display the half of the enames in upper case and remainin lower case
select concat ( upper ( substr ( ename, 0 , len8th (ename)= 2) ),
lower (substr (ename, len8th(ename) = 2+1, len8th(ename) )) ) from
emp;
1"2. display the 1!
th
record of emp table without usin roup by and rowed
1"3. Delete the 1!
th
record of emp table.
1"4. 0reate a copy of emp table.
reate table emp1 as select * from emp;
244549367.doc Page 15 of 18
QUERIES
1"5. )elect ename if ename e+ists more than once.
select d'st'nct(ename) from emp e where ename 'n(select ename from
emp where e:empnoC%empno);
1"6. display all enames in re'erse order.
select ename from emp order b/ ename desc;
1"7. Display those employee whose joinin of month and rade is eCual.
select empno,ename from emp e, sal8rade s where e:sal between
s:losal and s:h'sal and to4char(h'redate,9mm9)=8rade;
1"". Display those employee whose joinin date is a'ailable in dept no
select * from emp where to4char(h'redate,9dd9)=deptno;
1"#. Display those employees name as follows * *,,-/$ @ @,*2-
select substr(ename,1,1)II9 9IIename from emp;
1#!. ,ist out the employees ename$ sal$ 5< from emp
(elect ename,sal,sal*11=100 ?@ from emp;
1#1. Display 1)5) from emp without usin updatin$ insertin
1#2. 0reate table emp with only one column empno
reate table emp (empno number(1));
1#3. *dd this column to emp table ename Larchar%2!&.
alter table emp add ename varchar2(20) not null;
1#4. ??5)M B forot to i'e the primary key constraint. *dd it now.
alter table emp add constra'nt emp4empno pr'mar/ 6e/ (empno);
1#5. /ow increase the lenth of ename column to 3! characters.
alter table emp mod'f/ ename varchar2(&0);
1#6. *dd salary column to emp table.
alter table emp add sal number(K,2);
1#7. B want to i'e a 'alidation sayin that sal cannot be reater 1!$!!!%note i'e a
name to this column&.
alter table emp add constra'nt emp4sal4chec6 chec6 (salC10000);
1#". <or the time bein B ha'e decided that B will not impose this 'alidation. .y
boss has areed to pay more than 1!$!!!.
)lter table emp d'sable constra'nt emp4sal4chec6;
1##. .y boss has chaned his mind. /ow he doesn=t want to pay more than
1!$!!!. )o re'oke that salary constraint
)lter table emp enable constra'nt emp4sal4chec6;
2!!. *dd column called as mr to your emp table.
)lter table emp add m8r number(1);
244549367.doc Page 16 of 18
QUERIES
2!1. ?hM 4his column should be related to empno. Ei'e a command to add this
constraint
)lter table emp add constra'nt emp4m8r fore'8n 6e/(empno);
2!2. *dd dept no column to your emp table
)lter table emp add deptno number(&);
2!3. 4his dept no column should be related to deptno column of dept table
)lter table emp1 add constra'nt emp14deptno fore'8n 6e/(deptno)
references dept(deptno);
2!4. 0reate table called as new emp. ;sin sinle command create this table as well
as to et data into this table %use create table as&
create table newemp as select *from emp;
2!5. 0reate table called as newemp. 4his table should contain only empno$ename$
dname
create table newemp as select empno,ename,dname from emp e , dept
d where e:deptno=d:deptno;
2!6. Delete the rows of employees who are workin in the company for more than 2
years.
Aelete from emp where floor(s/sdate0h'redate)%2*&21;
2!7. 5ro'ide a commission to employees who are not earnin any commission.
update emp set comm=&00 where comm 's null;
2!". Bf any employee has commission his commission should be incremented by
1!8 of his salary.
update emp set comm=comm*10=100 where comm 's not null;
2!#. Display employee name and department name for each employee.
select ename,dname from emp e, dept d where e:deptno=d:deptno;
21!. Display employee number$ name and location of the department in which he is
workin.
(elect empno, ename, loc from emp e, dept d where
e:deptno=d:deptno;
211. Display ename$ dname e'en if there no employees workin in a particular
department%use outer join&.
(elect ename, dname from emp e, dept d where e:deptno (+)=
d:deptno;
212. Display employee name and his manaer name.
(elect e:ename, m:ename from emp e, emp m where e:m8r=m:empno;
213. Display the department name alon with total salary in each department.
(elect deptno, sum(sal) from emp 8roup b/ deptno;
244549367.doc Page 17 of 18
QUERIES
214. Display the department name and total number of employees in each
department.
select deptno,count(*) from emp 8roup b/ deptno;
215. *lter table emp1 add constraint emp1Ideptno forein key%deptno& references
dept%deptno&
216. Delete from emp where job name is clerk
217. Bnsert into emp without i'in any further commands mo'e to another client
system and lo into the same user i'e the followin command
21". *re the abo'e chanes reflected in this user
21#. Eo to your fist system and i'e commit come back to the second system and
i'e the followin command
22!. Display the current date and time
select to4char(s/sdate,9month mon dd // //// hhMm'Mss9) from dual;
244549367.doc Page 18 of 18

Das könnte Ihnen auch gefallen