Oracle Queries

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 19
At a glance
Powered by AI
The document provides examples of various SQL queries to retrieve data from database tables.

Some examples of SQL queries provided include queries to select, where, group by, order by, join etc. to retrieve data from tables.

The document discusses various SQL functions like TO_CHAR, SUBSTR, LENGTH, FLOOR etc. that can be used in queries.

Prepared by : Ramakrishna

SQL
1. To display the dept information from department table

Select * from dept;


2. To display the details of all employees

Select * from emp;


3. To display the ename and job for all employees

Select ename,job from emp;


4. To display name and salary for all employees

Select ename ,sal from emp;


5. To display employee number and total salary for each employee

Select empno,sal+comm from emp;


6. To display employee name and annual salaru for all employees

Select empno,ename,12*nvl(comm,0) annualsal from emp;


7. To display the names of all employees who are working in department number 10?

Select ename from emp where deptno=10;


8. To display the names of all employees workings as clerks and drawing a salary more
than 3000?

Select ename from emp where job=’clerk’ and sal>3000;


9. To display employee number and names for employees who earn commission

Select empno,ename from emp where comm is not null and comm.>0;
10. To display name of employees who do not earn any commission.

Select emp,ename from emp where comm is null and comm.=0;


11. To display the names of employees who are working as clerk,salesman or analyst and
drawing a salary more than 3000.
12. Select ename from employee(job=’clerk’ or job=’salesman’ or job=’analyst’) and
salary >3000;
13. (or)
select ename from emp where jobzzzzzzzzzzzzzzzzzzz
in(‘clerk’,’salesman’,’analyst’) and sal>3000;
14. To display the names of employee who are working in the company fast five years

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select ename from emp where sysdate-hiredate>5*365;
15. To display the list of wmployees who have joined the company before 30th june 90 or
after 31st dec 90.

Select * from emp where hiredate between “30-jun-1990’ and ’31-dec-1990’;


16. To display current sate.

Select sysdate from dual;


17. To display the list of users in your database (using log table)

Select * from dba_users;


18. To display the names of all tables from the current user

Select * from tab;


19. To display the name of current user?

Show user
20. To display the names of employees workin in department number 10 or 20 or 40 or
employees working as clerks ,salesman or analyst?

Select ename from employee where deptno in(10,20,40) or job in


(‘CLERK’,’SALESMAN’,’ANALYST’);
21. To display the names of employees whode name starts with alphabet s

Select ename from emp where ename like ‘%s’;


22. To display employee names for employees whose name ends with alphabet s

Select ename from emp where ename like ‘%s’


23. To display the names of employees whose names have second alphabet A in their
names

Select ename from emp where ename like ‘_s%’;


24. To display the names of employees whose name is exactly five characters in length

Select ename from emp where length(ename)=5; (or) select ename from emp where
ename like ‘_____’;
25. To display the names of employees who are not working as managers
26. Select * from emp minus (select * from emp where empno in (select mgr from emp));
27. (or)

28. Select * from emp where empno not in(select mgr from emp where mgr is not null);

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
29. (or)

Select * from emp e where not in(select mgr from emp where e.empno=’mgr’);
30. Display the ename of employees who are not woeking as salesman or clerk or analyst

Select ename from emp where job not in(‘clerk’,’salesman’,’analyst’);


31. Display all rows from emp table .the system should wait after every screen full of
information.

Set pause on;


32. To display the total number of employees working in the company.

Select count(*) from emp;


33. To display the total salary being paid to all employees.

Select sum(sal)+sum(nvl(comm.,0)) from emp;


34. To display the maximum salary from emp table.

Select max(sal) from emp;


35. To display the minimum salary from emp table.

Selecrt min(sal) from emp;


36. To display the maximum salary being paid to clerk.

Select avg(sal) from emp;


37. To display the maximum salary being paid to clerk.

Select max(sal) from emp where job=’clerk’;


38. To display the maximum salary being paid in dept no 20.

Select max(sal) from emp where deptno=20;


39. To display the min dal being paid to any salesman.

Select min(sal) from emp where job=’salesman’


40. To display the average salary drawn by managers

Select avg(sal) from emp where job=’manager’;


41. To display the total salary drawn by analyst working in deptno 40.

Select sum(sal),sum(nvl(comm.,0)) from emp where deptno=40;


42. Display the names of employees in order of salary i.e. the name of the employees
earning lowest salar should appear first.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select ename from emp order by sal;
43. To display the names of employees in descending order of salary.

Select ename from emp order by sal desc;


44. To display the details from emp table in order of emp name.

Select ename from emp order by ename;


45. To display empno,ename,deptno and sal.sort the output first based omn name and
within name by deptno and within deptno by sal;

Select * from emp order by ename,deptno,sal;


46. To display the name of the employee along with their annual salary (sal *12) . the
name of the employee earning highest annual salary should appear first.

Select ename,12*(sal+nvl(comm.,0)) annual from emp order by 12*(sal+nvl(comm.,0))


desc;
47. To display name,sal,hra,pf,da,total sal for each employee.the output should be in the
order o;f total sal,hra15% of sal,da 10% of sal, pf 5% of sal total salary will be
(sal*hra*da)-pf.
48. Select ename,dal,dal*15/100HRA,sal*5/100 PF,sal*10/100 DA,sal+sal*15/100-
sal*5/100+sal*10/100 total_salary from emp;
49. To display dept numbers and total number of employees within each group.

Select deptno,count(*) from emp group by deptno;


50. To display the various jobs and total mumber of employees with each job group.

Select job,count(*) from emp group by job;


51. To display department numbers and total salary for each department.

Select deptno,sum(sal) from emp group by deptno;


52. Display department numbers and maximum salary for each department.

Select deptno,max(sal),min(sal) from emp group by deptno;


53. To display the various jobs and total salary for each job.

Select job,sum(sal) from emp gtroup by job;


54. To display each job along with minimum sal being paid in each job group.

Select job,min(sal) from emp group by job;


55. To display the department numbers with more than three employee in each dept.

Select deptno,count(*) from emp group by deptno having count(*)>3;

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
56. To display the various jobs along with total sal for each of the hobs where total sal is
greater then 40000.

Select job,sum(sal) from emp group by job having sum(sal)>40000;


57. To display the various jobs along with total number of employees in each job.the
output should contain only those jobs with more than three employees.

Select job,count(*) from emp group by job having count(*)>3;


58. To display the name of emp who earns highest sal.

Select ename from emp where sal=(select max(sal) from emp);


59. To display the employee number and name of employee working as clerk amd earning
highest salary among clerks.
60. Select empno,ename from emp where job=’CLERK’ and sal=(select max(sal) from
emp where hob=’CLERK’);
61. To display the names of the salesman who earns a salary more than the highest salary
of any clerk.
62. Select ename from emp where job=’SALESMAN’ and sal>(select maz(sal) from
emp where job=’CLERK’);
63. Display the names of clerks who earn salary more than that of james of that of sal
lesser than that of scott.
64. Select ename from emp were job=’CLERK’ and sal<(select sal from emp where
ename=’SCOTT’) and sal>(select sal from emp where ename=’JAMES’);
65. To display the names of employees who earn a sal more than that of james or that of
salary gteater than that of scott.

Select ename from emp where sal<(select sal from emp where ename=’SCOTT’) and
sal>(select sal from rmp where ename=’JAMES’);
66. To display the names of the emploees who earn highest salary in their respective
departments.

Select * from emp e where sal=(select max(sal) from emp where deptno=e.deptno);
67. To display the names of employees who earn highest salaries in thir respective job
groups.

Select * from emp e where sal in (select max(sal) from emp group by job having
e.job=job);
68. To display the employee names who are working in accountging dept.

Select ename from emp where deptno in(select deptno from dept where
dname=’ACCOUNTING’);
69. To display the employee names who are working in Chicago.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select ename from emp where deptno=(select deptno from dept where
loc=’CHICAGO’);
70. To display the hob groups having total salary greater then the maximum salary for
managers.

Selct job,sum(sal) from emp group by job having sum(sal)>(select max(sal) from emp
where job=’MANAGER’);
71. To display the names of employees from department number 10 with salary greater
than that of any employee working in other departments.

Select ename,sal,deptni from emp e where deptno=10 and sal>any(select sal from emp
where e.deptni!=deptno);
72. Display the names of employees from department number 10 with salaty greater then
that of all employees working in other departments.

Select ename,sal,deptno from emp e where deptno=10 and sal>any(select sal from emp
where e.deptno!=deptno);
73. To display the names of employees in upper case.

Select upper(ename) from emp;


74. To display the names of employees in lower case.

Select lower(ename) from emp;


75. To display the names of employees in proper case.

Select initcap(ename) from emp;


76. To display the length of your name using appropriate function.

Select length(‘india’) from dual;


77. To display the length of all employees names.

Select sum(length(ename)) from emp;


78. To display the name of the employee concatenate with empno.
79. Select ename||empno from emp;

80. (or)

Select concat(ename,empno) from emp;


81. Use appropriate function and extract 3 characters starting from 2 characters from the
following string ‘oracle’ i.e. the output should be ‘rac’.

Select substr(‘oracle’,2,3) from dual;

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
82. Find the first occurrence of character a from the following string ‘computer
maintenance corporation’.

Select instr(‘computer maintenance corporation’,’a’,1,1) from dual;


83. Replace every occurrence of alphabet Awith B in the string allen’s (user translate
function).

Select replace(‘allens’,’A’,’b’) from dual;


84. To display the information from emp table wherever job ‘manager’ is found it should
be displayed as boss(replace function).

Select empno,ename,replace(job,’MANAGER’,’Boss’) job from emp;


85. To display empno,ename,deptno from emp table instead 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;


86. To display your age in days.

Select round(sysdate-to_date(’15-aug-1947’)) from dual;


87. To display your age in months.

Select floor(months_between(sysdare,’15-aug-1947’)) “age in months” from dual;


88. To display current date as 15th august Friday nineteen forty seven.

Select to_char(sysdate,’ddth month day year’) from dual;


89. To display the following output for each row from emp table as ‘scott has joined the
company on Wednesday 13th august nineteen ninety’.

Select ename||’has joined the company on ‘|| to_char (hiredate,’day ddth month year’)
from emp;
90. To display the date of nearest saturday after current day.

Select next_day(sysdate,’SATURDAY’) from dual;


91. To display current time.

Select to_char(sysdate,’hh:mi:ss’) time from dual;


92. To display the date three months before the current date.

Select add_months(sysdate,-3) from dual;


93. To display the common jobs from department number 10 and 20 .

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
94. Select job from emp where deptno=10 and fob in(select job from emp where
deptno=20); (or)

Select job from emp where deptno=10 intersect select job from emp where
deptno=20;
95. To display the jobs found in department numer 10 and 20 eliminate duplicate jobs.
96. Select distinct(job) from emp where deptno=10 and job in(select job from emp where
deptno=20);
(or)

Select job from emp where deptno=10 intersect job from emp where deptno=20;
97. To display the jobs which are unique to deptno 10.
98. Select job from emp where deptno=10 minus select job from emp where deptno!=10;
(or)

Select job from emp where deptno=10 and job not in (select job from emp
where deptno<>10);
99. To display the details of those who do not have any person working under them.

Select empno from emp where empno not in(select mgr from emp where mgr is not
null);
100. To display the details of employees who are in dept and grade is 3.
101. Select * from emp where sal>=(select losal from salgtade where grade=3) and
102. sal<=(select hisal from salgrade where grade=3) and deptno=(select
deptno from
dept where dname=’SALES’);
103. To display those who are not managers and who are manager any one.

Select * from emp where empno in(select mgr from emp where mgr is not null);
104. To display tose employees whose name contains not less than 4 chars.

Select * from emp where length(ename)>4;


105. To display those departments whode name start with ‘s’ while location name
wnd with ‘0’.

Select * from depdt where dname like ‘s’ and loc like’o’;
106. To display thode emplouees whode manager name is JONES.

Select * from emp where mgr=(select empno from emp where ename=’JONES’);
107. Display those employees whode salary is more than 3000 after giving 20%
increment .

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
108. Select * from emp where sal*120/100>3000;

109. (or)

Select * from emp where sal+sal*20/100>3000;


110. To display all employees with thee dept name.

Select ename,dname from emp e,dept d wher e.deptno=d.deptno;


111. To display ename who are working in dept.

Select empno,ename from emp where deptno=(select deptno from dept where
dname=’SALES’);
112. To display employee name,deptname,salary and comm. For those sal in
between 2000 and 5000 while location is Chicago.

Select empno ,ename,deptno from emp where deprno=(select deptno from dept where
loc=’CHICAGO’) and sal between 2000 and 5000;
113. To display those employees whose salaru greater than his manager salary.

Selecr * from emp e where sal>(selecr sal from emp where empno=e.mgr);
114. To display those employees who are working in the same dept where his
manager is working .

Select * from emp e where deptno=(select deptno from emp where empno=e.mgr);
115. To display those emplouees who are not working under any manager.

Select * from emp where mgr is null or empno=mgr;


116. To display grade and employees name for the dept no 10 or 30 but grade is not
4,while joined the company before 31-dec-82.
117. Select empno,ename,sal,deptno,hiredate,grade from emp e,salgrade s where
e.sal>=s.losal and e.sal<=s.hisal and deptno in (10,30) and
grade<>4 and hiredate<’01-dec-1981’;
118. To update the salary of each employee by 10% increments that are not eligible
for commission.

Update emp set sal=sal+(sal*10/100) where comm is null;


119. To delete those employees who hoined the company before 31-dec-82 while
there dept location is ‘NEW YORK’ or ‘CHICAGO’.

Delete from emp where hiredate<’31-dec-1982’and deptno in(select deptno from dept
where loc in(‘NEW YORK’,’CHICAGO’));

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
120. To display employee name,job,deptname,location for all who are working as
managers.

Select ename,job,dname,loc from emp e,dept dwhere e.deprno=d.deptno and empno


in(select mgr from emp);
121. To display those employees whode manager names is jones and also display
there manager name.

Select e.empno,e.ename,m.ename MANAGER from emp e, emp m where


e.mgr=m.empno and m.ename=’JONES’;
122. To display name and salary of ford if his sal is equal to high sal of his grade.
123. Select ename,sal from emp e where ename=’FORD’ and sal=(select hisal from
salgrade where grade=(select grade from salgrade where
e.sa>=losal and e.sal<=hisal));
124. To display employee name,his job,his dept name,his manager name,his grade
and make out of an under department wise Break d.deptno;
125. Select d.deptno,e.ename,e.job,d.dname,m.ename,s.grade from emp e, emp m,dept d,
salgrade s where e.deptno=d.deptno and e.sal between
s.losal and s.hisal and e.mgr=m.empno order by e.deptno;
126. To display all the employees name ,job,and salar grade and department name
for every one in the company except’CLERK’ sort on salary display the highest salary.
127. Select empno,ename,sa,dname,grade from emp e,dept d, dalgrade s where w.deptno-
d.deptno and e.sal berween s.losal and s.hisal and
e.job<>’CLERK’ order by sal;
128. To display employee name ,his job and his manager .display also emplyoees
who are without manager.
129. Select e.ename,e.job,m.ename manager from emp e,emp m where e.mgr=m.empno
union select ename,job ,’no manger’ from emp where
manager is null;
130. To display the top 5 earner of company .

Select * from emp e where 5>(select count(*) from emp where sal>e.sal) order by sal
desc;
131. To display the name of those employees who are getting highest salary.

Select empno,ename,sal from emp where sal=(select max(sal) from emp);


132. To display those employees whose salary id equal to average of maximum and
minimum.

Select * from emp where sal+(select(max(sal)+min(sal))/2 from emp);


133. To display count of employees in each department where count greater than 3.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select deprno,count(*) from emp group by deptno having count(*)>3;
134. To display dname where at least 3 are working and display only dname.

Select dname from dept where deptno in(select deptno from emp group by deptno
having count(*)>3);
135. To display name of those manager name whose salary is more than average
ssalary of company.

Select ename,sal from emp where empno in(select mgr from emp) and sal>(select
avg(sal) from emp);
136. To display those managers name whose salary is more than an average salary
of his employees.

Select ename,sal from emp e where empno in(select mgr from emp) and e.sal>(select
avg(sal) from emp where mgr=e.empno);
137. To display wmployee name,sal,comm. and net pay for those employees whose
net pay are greater than or equal to any other employee salary of the company.

Select ename,sal,comm.,sal+nvl(comm.,0)netpay from emp where


sal+nvl(comm.,0)>+any(select sal from emp);
138. To display those emploiyees whose salary is less than his manager but more
than salary of any other managers.

Select * from emp e where sal<(select sal from emp where empno=e.mgr) and sal>any
(select sal from emp where empno!=e.mgr);
139. To display the last 5(least) earner of the company.

Select * from emp e where 5>(select count(*) from emp where sal<e.sal) order by sal;
140. To display the number of employees whose salary is greater than there
manager salary

Select count(*) from emp e where sal>(select sal from emp where cempno=e.mge);
141. To display those manager who are not working under president but they are
working under any other manager.

Select * from femp e where mge in(select empno from emp where ename ‘KING’);
142. To delete those department where no employees working.

Delete from dept d where 0=(select coountA(A*) from rmp where deptno=d.deptno);
143. To delete those records frtom emp table whose deptno not available in dept
table.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Delete from emp where deptno not in(select deptno from dept);
144. To display those earners whose salary is out of the grade available in sal grade
table.

Select * from emp where sal<(select min(losal) from salgrade) or sal>(select max(sal)
from salgrade);
145. To display employee name, sal ,comm. and whose net pay is greater than any
other in the company.
146. Selecrt ename,sal ,comm, from emp wher sal+sal*15/100-
sal*5/100+sal*10/100=(select max(sal+sal*15/100-sal*5/100+sal*10/100) from
emp);
147. To display name of those employees who are going to retire 31-dec-99. if the
maximum job is period is 18 years.

Select * from emp where (to_date(“31-dec-1999’)-hiredate)/365>18;


148. To display those employees whose salary is ODD value.

Select * from emp where mod(sal,2)=1;


149. To display those emplouees whose salary contains at least 4 digits.

Selecrt * from emp ehere length(sal)>=4;


150. To display those employees who joined in the company in the company in the
months of dec.

Select * from emp where upper(to_char(hiredate,’mon’))=’DEC’;


151. To display those employees whose name contains “A”.

Select * from emp where instr (ename,’A’,1,1,)>0;


152. To display those employees whose deptno is available in salary.

Select * from emp where instr(sal,deptno,1,1)>0;


153. To display those employees whose first 2 characters from hiredare-last 2
characters of salary.

Select concat(substr(hiredate,0,2),substr(sal,lingth(sal)-1,2)) from emp;


154. To display those employee whose 10% of salary is equal to the year of joining.

Select * from emp where to_vhar(hiredate,’yy’)=sal*10/100;


155. To display those employees who are working in sales or research.

Select * from emp where deptno in(select deptno from dept where dname
in(‘SALES’,RESEARCH’));

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
156. To display the grade of jones.
157. Select grade from salgrade where losal<=(select(sal) from emp where
ename=’JONES’) and hisal>+(select(sal) from emp where
ename=’JONES’);
158. To display those employees who joined the company before 15th of the month.

Select empno,ename from emp where hiredate<(to_date(’15-‘||


to_char(hiredate,’mon’)||’-||to_char(hiredate,’yyyy’)));
159. To delete those records where no o femployee in a particular department is less
than 3.

Delete from emp where deprno in(select deptno from emp group by deptno having
count(*)>3);
160. To delete those employees who hoined the company 21 years back from today.
161. Select * from emp where round((sysdate-hiredate/365)>21;
162. (or)

Select * from emp wher (to_char(sysdate,’yyyy’)-


to_char(hiredate,’yyyy’))>21;
163. To display the department name the no of characters of which is equal to no of
employees in any other department.

Select dname from dept where length(dname) in(select count(*) from emp group by
deptno);
164. To display those employees who are working as manager.

Select * from emp where empno in(select mgr from emp);


165. To count the no of employees who are working as manager (use set operation).

Select count(*) from emp where empno in(select mgr from emp);
166. To display the name of then dept those employees who joined the company on
the same date.

Select empno,ename,hiredate,deptno from emp e where hiredate in (select hiredate


from emp where empno<>e.empno);
167. To display the manager who is having maximum number of employees working
under him.

Select mgr from emp group by mgr having count(*)=(select max(count(mgr)) from
emp group by mgr);
168. To display employeesname and salary increased by 15% and expredded as
whole number of dollars.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select empno,ename,lpad(concat(‘$’,round(sal*115/100),7) salary from emp;
169. Produce the output of the emp table “EMPLOYEE_AND_JOB” for ename and job.

Select * from “EMPLOYEE_AND_JOB”;


170. To display all employees with hiredate in the format’june 4 1988’.

Select to_charA(hiredate,’month dd yyyy’) from emp;


171. To display a list of employees displaying ‘less salary ‘ if less than 1500 if exactly
1500 display as ‘exact salary ‘ and if greater than 1500 display ‘more salary’.
172. Select emp,ename,’less salary’||sal from emp where sal<15oo

173. Union

174. Select emp,ename,’more salary’||sal from emp where sal<15oo

175. Union

Select emp,ename,’exact salary’||sal from emp where sal<15oo;


176. To display to calculate the length of employee has been with the company.

Select round(sysdate-hiredate) from emp;


177. To display those managers who are getting less than his employees sal.

Select empno from emp e where sal<any(select sal from emp where mgr=e.empno);
178. To display the details of all the employees who are sub ordinate to blake.

Select * from emp where mgr=(select empno from emp where ename=’BLAKE’);
179. To display those who working as manager using co related sub query.

Select * from emp where empno in(select mgr from emp);


180. To display those employees whose manager name is hones and also with his
manager name.
181. Select * from emp wher mgr=(select empno from emp where ename=’jones’)

182. union

Select * from emp where empno=(select mgt from emp where ename=’JONES’);
183. To define variable represenring the expressions used to calculate on employee’s
total Annual renumaration.

Define efmp_ann_sal=(sal+nvl(comm.,0))*12;

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
184. To use the variable in a statement which finds all employees who can earn
30000 a year or more.

Select * from emp where &emp_ann_sal>30000;


185. To dispaly the how many manager are there with out listing them.

Select count(*) from empwhere empno in (select mgr from emp);


186. To display the avg sal and avg total remuneration for each job type remember
salesman earn commission.

Select job,avg(sal+nvl(comm.,0)),ssum(sal+nvl(comm.,0))from emp group by job;


187. To check whether all employees number are indeed unique.

Selecr count (empno),count(distinct (enmpno)) from emp having


count(empno)=(count(distinct(empno)));
188. To display the lowest paid employees working for each manager ,exclude any
groups where min sal is less than 1000 sort the output by sal.

Select e.ename,e.mgr,e.sal from emp e where sal in (select min(sal) from emp where
mgr=e.mgr) and e.sal>1000 order by sal;
189. To display ename ,job ,annual sal,deptno,dname and grade who earn 30000 per
year and who are not clerk.
190. Select e.ename,e.job,(e.sal+nvl(e.comm,0))*12,e.deptno, d.dname,s.grade from emp
191. e,salgrade s ,dept d where e.sal between s.losal and s.hisal and e.deprno=d.deptno
and(e.sal+nvl(comm.,0))*12>30000 and e.job<>’CLERK’;
192. To display the all employees who joined the company before their manager .

Select * from emp e where hiredate <(select hiredate from emp where empno=e.mgr);
193. To display the all employees by name and number along with their managerd
name and number also display ‘no manager’ who has no manager.
194. Select e.empno,e.ename ,m.empno manafer,m.ename managername from emp e.emp
m where e.mgr=m.empno

195. Union

Select empno ,ename,mgr,’no manager’ from emp where mgr is null;


196. To display the employees who earned the highest sal in each job typed sort in
descending sal order.

Select * from emp e where sal=(select max(sal) from emp where job=e.job);
197. To display the employees who earned the min sal for their job in ascending
order.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Select * from emp e where sal=(select min(sal) from emp where job=e.job) order by sal;
198. To display the most recently hired employees in each dept order by hiredate.

Select * from emp order by deptno,hiredate desc;


199. To display ename,sal and deptno for each employee who earn a sal greater
than the avg of their department order by deptno.

Select ename,sal,deptno from emp e where sal>(select avg(sal) from emp where
deptno=e.deptno) orser by deptno;
200. To display the department where there are no employees.

Select deptno,dname from dept where deptno not in(select distinct (deptno) from
emp);
201. To display the dept no with highest annual remuneration bill as compensation.

Select deptno,sum(sal) from emp group by deptno having sum(sal)=(select


max(sum(sal)) from emp group by deptno);
202. To display in which year did most people join the company . display the year
and number of employees.

Select count(*) ,to_char(hiredate,’yyyy’) from emp group by to_char(hiredate,’yyyy’);


203. To display avg sal figure for rthe dept.

Select deptno,avg(sal) from emp group by deptno;


204. To display against the row of the most recently hired employee. Display ename
hire date and column max date showing.

Select empno,hiredate from emp where hiredate=(select max(hiredate) from emp);


205. To display employee who can earn more than lowest sal in dept no 30.

Select * from emp where sal>(select min(sal) from emp where deptno=30);
206. To display employees who can earn more than every employees in deptno 30.

Sleect * from emp where sal>(select max(sal) from emp where deptno=30);
207. To display avg sal and avg total remainders for each job type.

208. To display all dept which have more than 3 empoyees

Select deptno from emp group by deptno having count(*)>3;


209. To display the half of the enames in upper cade and remaining lower cade .

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Selectconcat(upper(substr(ename,0,length(ename)/2)),
lower(substr(ename,lengtah(ename)/2+1,length(ename))))from emp;
210. create copy of emp table.

Select table emp1 as select * from emp;


211. To display ename if ename exists more than once

Select distinct(ename) from emp e where ename in (select ename from emp where
e.empno<>empno);
212. To display all enames in reverse order

Select ename from emp order by ename desc;


213. To display rthose employee whose joining of month and grade is equal.

Select empno,ename from emp e,salgrade s where e.sal between s.losal and s.hisal and
to_char(hiredate,’mm’)=grade;
214. To display those employee whose hoining date is available in deptno .

Select * from emp where to_char(hiredate,’dd’)=deptno;


215. To display those employees name as follows A ALLEN,B BLAKE.

Select substr (ename,1,1)||’’|| ename from emp;


216. To display the employees ename ,sal,pf from emp.

Select ename,sal,sal*15/100 pf from emp;


217. To create a table emp with only one column empno

Create table emp(empno number(9));


218. To add column to emp table ename varchar2(20)

Alter table emp add ename varchar2(20) not null;


219. To give the primary key constraint

Alter table emp add constraint emp_empno primary key(empno);


220. To increase the length of ename column to 30 characters

Alter table emp modify ename varchar2(30);


221. add salary column to emp table

Alter table emp add sal number(10,2);


222. I want to give a validation saying that sal cannot be greater 10,000(note give a
name to this column)

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Alter table emp add constraint emp_sal_check check (sal<10000);
223. for the time being I have decided that I will not impose this validation. My boss
has agreed to pay more than 10,000

Alter table emp disable constraint emp_sal_check;


224. my boss has changed his mind. Now he doesn’t want to pay more than 10,000.
so revoke that salary constraint

Alter table emp enable constraint emp_sal_check;


225. add column called as mgr to your emp table

Alter table emp add mgr number(9);


226. oh! This column should be related to empno. Give a command to add this
constraint

Alter table emp add constraint emp_mgr foreign key(empno);


227. add deptno column to emp table

Alter table emp add deptno number(10);


228. This dept no column should be related to deptno column of dept table

Alter table emp1 add constraint emp1_deptno foreign key(deptno) References


Dept(deptno);
229. Create table called as new emp, Using single command create this table As well
as to get data into this table (use create table as)

create table newenp as Select * from emp;


230. Create table called as new emp. This 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;
231. Delete the rows of employees who are working in the company for more Than
two years.

delete from emp where floor(sysdate-hiredate)>2*365;


232. provide a commission to employees who are not earning any commission.

update emp set comm=300 where comm Is null;


233. If any employee has commission, his commission should be incremented by 10%
of his salary.

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)
Prepared by : Ramakrishna
Update emp set comm=comm*10/100 where comm Is not null;
234. Display employee name and department name for each employee.

Select ename,dname from emp e,dept d where e.deptno=d.deptno;


235. Display employee number,name and location of the department in which he is
working.

select empno,ename,loc from emp e,dept d where e.deptno=d.deptno;


236. Display ename,dname even if there no employees working in a particular
department(use outer join).

select ename,dname from emp e,dept d where e.deptno(+)=d.deptno;


237. Display employee name and his manager name.

Select e.ename,m.ename from emp e,emp m where e.mgr=m.empno;


238. Display the department name along with total salary in each department.

Select deptno,sum(sal) from emp group by deptno;


239. Display the department name and total number of employees in each
department.

Select deptno,count(*) from emp group by deptno;


240. Display the current date and time.

Select to_char(sysdate,’month mon dd yy yyyy hh:mm:ss’) from dual;






 Note: No rows are released from aggregator until all rows are aggregated

SRM IT SOLUTIONS
# 153, Prashanth hills colony, Khajaguda, Hyderabad.
( Gmail id: ramkrishnapcm@gmail.com, phone & WhatsApp: +918919090704)

You might also like