SQL
SQL
DEPTNO NUMBER(2),
DNAME VARCHAR2(14),
LOC VARCHAR2(13));
INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK', 7902, TO_DATE('17-DEC-1980', 'DD-
MON-YYYY'), 800, NULL, 20);
INSERT INTO EMP VALUES (7499, 'ALLEN', 'SALESMAN', 7698, TO_DATE('20-FEB-1981',
'DD-MON-YYYY'), 1600, 300, 30);
INSERT INTO EMP VALUES (7521, 'WARD', 'SALESMAN', 7698, TO_DATE('22-FEB-1981', 'DD-
MON-YYYY'), 1250, 500, 30);
INSERT INTO EMP VALUES (7566, 'JONES', 'MANAGER', 7839, TO_DATE('2-APR-1981', 'DD-
MON-YYYY'), 2975, NULL, 20);
INSERT INTO EMP VALUES (7654, 'MARTIN', 'SALESMAN', 7698,TO_DATE('28-SEP-1981',
'DD-MON-YYYY'), 1250, 1400, 30);
INSERT INTO EMP VALUES (7698, 'BLAKE', 'MANAGER', 7839,TO_DATE('1-MAY-1981', 'DD-
MON-YYYY'), 2850, NULL, 30);
INSERT INTO EMP VALUES (7782, 'CLARK', 'MANAGER', 7839,TO_DATE('9-JUN-1981', 'DD-
MON-YYYY'), 2450, NULL, 10);
INSERT INTO EMP VALUES (7788, 'SCOTT', 'ANALYST', 7566,TO_DATE('09-DEC-1982', 'DD-
MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES (7839, 'KING', 'PRESIDENT', NULL,TO_DATE('17-NOV-1981', 'DD-
MON-YYYY'), 5000, NULL, 10);
INSERT INTO EMP VALUES (7844, 'TURNER', 'SALESMAN', 7698,TO_DATE('8-SEP-1981', 'DD-
MON-YYYY'), 1500, 0, 30);
INSERT INTO EMP VALUES (7876, 'ADAMS', 'CLERK', 7788,TO_DATE('12-JAN-1983', 'DD-
MON-YYYY'), 1100, NULL, 20);
INSERT INTO EMP VALUES (7900, 'JAMES', 'CLERK', 7698,TO_DATE('3-DEC-1981', 'DD-MON-
YYYY'), 950, NULL, 30);
INSERT INTO EMP VALUES (7902, 'FORD', 'ANALYST', 7566,TO_DATE('3-DEC-1981', 'DD-
MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES (7934, 'MILLER', 'CLERK', 7782,TO_DATE('23-JAN-1982', 'DD-
MON-YYYY'), 1300, NULL, 10);
3) Display all the records in emp table where employee does not belong to deptno
30?
10) Display the records in emp table where MGR in 7698,7566 and sal should be
greater then 1500
select * from emp where mgr in(7698,7566) and sal > 1500
14) Display all employees where their hiredate belongs to third quarter?
select * from emp where to_char(hiredate,'Q') = 3;
15) Display all employees where their salary is less then the Ford’s salary?
select * from emp where sal <(select sal from emp where ename='FORD');
16) Display all the records in EMP table along with the rowid?
17) Display all records in EMP table those were joined before SCOTT joined?
select * from emp where hiredate <(select hiredate from emp where ename='SCOTT')
18) Display all employees those who were joined in third quarter of 1981?
19) Add 3 months with hiredate in EMP table and display the result?
24) Display all the records in emp table where employee hired after 28-SEP-81 and
before 03-DEC-81?
25) Write a query that displays the employee’s names with the first letter
capitalized and all other letters lowercase for all employees whose name starts
with J, A, or M
select initcap(ename) from emp where ename like 'J%' or ename like 'A%' or ename
like 'M%'
26) Display all jobs that are in department 10. Include the location of department
in the output.
select job, loc from emp,dept where emp.deptno = dept.deptno and emp.deptno =10
27) Write a query to display the employee name, department name of all employees
who earn a commission
select ename,dname from emp,dept where emp.deptno = dept.deptno and comm is not
null;
28) Display the empno, ename, sal, and salary increased by 15%.
select empno, ename, sal actual_sal, (sal * 15/100) as Increased_sal from emp
select ename,sal,grade from emp,salgrade where sal between losal and hisal;
31) Display all the departments where employee salary greater then average salary
of that department.
select ename,deptno, sal from emp a where sal > (select avg(sal) from emp where
emp.deptno = a.deptno) order by deptno;
32) Display all employees whose salary greater then the manager salary?
34) Display all employees where ename start with J and ends with S
35) Display all employees where employee does not belong to 10,20,40
36) Display all employees where jobs does not belong to PRESIDENT and MANAGER?
41) Display all ename where first character could be anything, but second character
should be L?
select * from emp where ename like '_L%'
42) Display nth highest and nth lowest salary in emp table?
SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM
EMP B WHERE a.sal<=b.sal);
select distinct sal from (select ename,sal,dense_rank() over(order by sal desc) dr
from emp) where dr = &x ;
43) Display all the departments where department has 3 employees?
select deptno from dept a where deptno in(select deptno from emp group by deptno
having count(*)=3)
44) Display emp name and corresponding subordinates. Use CONNECT BY clause.
select lpad(' ',level+12)+ename from emp connect by prior empno = mgr start with
mgr is null
Note: Please replace pipe symbol in the place of + sign for question 44. Pipe
symbol is not displaying the blog. This is the reason, i used Plus sign here.
45) Display sum of salary for each department. The output should be in one record
46) Display all department with Minimum salary and maximum salary?
47) Display all ename, sal, deptno,dname from emp, dept table where all department
which has employees as well as department does not have any employees. This query
should include non matching rows.
48) Display all ename, sal, deptno from emp, dept table where all employees which
has matching department as well as employee does not have any departments. This
query should include non matching rows.
Note: In the below query, employee will always have matching record in dept table.
Emp, dept table may not be good example to answer this question.
select dname,b.deptno, ename,sal from emp a, dept b where a.deptno = b.deptno(+);
select dname,b.deptno, ename,sal from emp a left outer join dept b on a.deptno =
b.deptno;
49) Display all ename, sal, deptno from emp, dept table where all employees which
has matching and non matching department as well as all departments in dept table
which has matching and non matching employees. This query should include non
matching rows on both the tables.
Note: In the below query, employee will always have matching record in dept table.
Emp, dept table may not be good example to answer this question.
select dname,b.deptno, ename,sal from emp a full outer join dept b on a.deptno =
b.deptno
50) Display all ename, empno, dname, loc from emp, dept table without joining two
tables
select * from emp,dept;
51) Display all the departments where department does not have any employees
select deptno from dept where not exists(select 1 from emp where emp.deptno =
dept.deptno);
select deptno from dept where deptno not in(select deptno from emp);
52) Display all the departments where department does have atleast one employee
select * from dept a where exists(select 1 from emp b where b.deptno = a.deptno)
select * from dept a where deptno in(select deptno from emp b where a.deptno =
b.deptno)
select ename from emp a where not exists (select 1 from emp b where b.mgr =
a.empno);
select ename from emp a where empno not in (select mgr from emp b where b.mgr =
a.empno and mgr is not null);
54) Display ename, deptno from emp table with format of {ename} belongs to {deptno}
Note: Please replace pipe symbol in the place of + sign for question 44. Pipe
symbol is not displaying the blog. This is the reason, i used Plus sign here.
55) Display total number of employees hired for 1980,1981,1982. The output should
be in one record.
select
count(decode(to_char(hiredate,'YYYY'), 1980,hiredate)) as "total no of employees
hired in 1980",
count(decode(to_char(hiredate,'YYYY'), 1981,hiredate)) as "total no of employees
hired in 1981",
count(decode(to_char(hiredate,'YYYY'), 1982,hiredate)) as "total no of employees
hired in 1982"
from emp
56) Display ename, deptno from employee table. Also add another column in the same
query and it should display ten for dept 10, twenty for dept 20, thirty for dept
30, fourty for dept 4
select employeename, (case
when salary<40000 then 'C grade'
when salary>40000 and salary<80000 then 'B grade'
when salary>800000 then 'A grade'
else 'invalid' end) as grade
from employee;
57) Display all the records in emp table. The ename should be lower case. The job
first character should be upper case and rest of the character in job field should
be lower case.
60) Display empno, deptno, salary, salary difference between current record and
previous record in emp table. Deptno should be in descending order.
select empno,
ename,
job,
sal,
lag(sal, 1, 0) over (order by sal) as "previous salary",
sal - lag(sal, 1, 0) over (order by sal) as "salary difference"
from emp;
61) Create table emp1 and copy the emp table for deptno 10 while creating the table
62) Create table emp2 with same structure of emp table. Do not copy the data
63) Insert new record in emp1 table, Merge the emp1 table on emp table.
MERGE
INTO emp tgt
USING emp1 src
ON ( src.empno = tgt.empno )
WHEN MATCHED
THEN
UPDATE
SET tgt.ename = src.ename,
tgt.job = src.job,
tgt.mgr = src.mgr,
tgt.hiredate = src.hiredate,
tgt.sal = src.sal,
tgt.deptno = src.deptno
WHEN NOT MATCHED
THEN
Insert(
Tgt.empno,
Tgt.Ename,
Tgt.Job,
Tgt.Mgr,
Tgt.Hiredate,
Tgt.Sal,
Tgt.Comm,
Tgt.Deptno)
values (src.empno,
src.ename,
src.job,
src.mgr,
src.hiredate,
src.sal,
src.comm,
src.deptno);
64) Display all the records for deptno which belongs to employee name JAMES?
select * from emp where deptno in(select deptno from emp where ename = 'JAMES')
65) Display all the records in emp table where salary should be less then or equal
to ADAMS salary?
select * from emp where sal <= (select sal from emp where ename='ADAMS')
66) Display all employees those were joined before employee WARD joined?
select * from emp where hiredate < (select hiredate from emp where ename='WARD')
67) Display all subordinate those who are working under BLAKE?
Select ename from emp where mgr = (select empno from emp where ename='BLAKE')
select ename from emp start with empno = (select empno from emp where
ename='BLAKE')
connect by prior empno = mgr
69) Display all record in emp table for deptno which belongs to KING's Job?
select * from emp where deptno in(select deptno from emp where job= (select job
from emp where ename = 'KING'))
70) Display the employees for empno which belongs to job PRESIDENT?
select * from emp where empno in(select empno from emp where ename in(select ename
from emp where JOB = 'PRESIDENT'));
71) Display list of ename those who have joined in Year 81 as MANAGER?
73) Display who is senior most employee? How many years has been working?
75) Display ename, sal, grade, dname, loc for each employee.
77) Display ename, job, dname, deptno for each employee by using INLINE view?
SELECT emp.ename,
emp.JOB,
emp.deptno,
dnames.dname
FROM emp
JOIN (select dname, deptno
from dept ) dnames ON emp.deptno = dnames.deptno
78) List ename, job, sal and department of all employees whose salary is not within
the salary grade?
79) Use EMP and EMP1 table. Query should have only three columns. Display
empno,ename,sal from both tables inluding duplicates.
select empno, ename, sal from emp
union all
select empno, ename, sal from emp1
81) Delete all employees those are not getting any commission?
delete emp where comm is null;
82) Delete all employees those who employeed more then 28 years
delete emp where trunc(sysdate - hiredate)/365 > 28;
83) Add duplicate records in emp1 table. Delete the duplicate records in emp1
table.
insert into emp1 select * from emp1 where rownum <=1; commit; delete emp1 a where
a.rowid <>(select min(b.rowid) from emp1 b where a.empno = b.empno);
84) Delete the employees where employee salary greater then average salary of
department salary?
delete emp a where sal > (select avg(sal) from emp where emp.deptno = a.deptno);
86) Delete all levels of employees those who are under BLAKE?
Delete emp where ename in(select ename from emp start with empno = (select empno
from emp where ename='BLAKE')
connect by prior empno = mgr)
87) Delete all employees those who are only managers?
delete emp where ename in(select ename from emp a where empno in (select mgr from
emp b where b.mgr = a.empno and mgr is not null))
88) Remove the department in dept table where dept does not have any employees?
delete dept where deptno not in(select deptno from emp where deptno is not null)
delete emp where empno in(select empno from emp,salgrade where sal between losal
and hisal and grade = 2)
delete emp where deptno = (select deptno from emp where ename = 'SMITH')
delete emp where sal = (select min(sal) from emp where mgr =
(select empno from emp where ename = 'BLAKE')) and
ename in(select ename from emp where mgr =
(select empno from emp where ename = 'BLAKE'))
92) Remove all employees who were joined before SMITH joined?
delete emp where hiredate < (select hiredate from emp where ename='SMITH');
93) Rename the employee name JONES to ANDY
update emp set sal = (select sal from emp where ename = 'SMITH') where
ename='MARTIN'
96) Increase the salary 5% for employee those who are earning commission less then
1000
update emp set sal = sal + (sal * (5/100)) where comm between 0 and 1000
update emp set comm = nvl(comm,0)+250 where mgr = (select empno from emp where
ename='BLAKE');
98) Increase 100$ for employee who is making more then averge salary of his
department?
update emp a set sal = sal + 150 where sal > (select avg(sal) from emp b where
b.deptno = a.deptno)
99) Increase 1% salary for employee who is making lowest salary in dept 10
100) Reduce the commission amount from employee salary for each employee who were
joined after ALLEN joined.
101) Increase commission 10$ for employees those who are located in NEW YORK.
--4
select max(salary),City from employee group by city having count(*)>10;
create view
deptview as
select departmentno, max(salary) from employee groupby departmentno;