SQL Queries Withsolutions
SQL Queries Withsolutions
1. Display empname, job, hiredate, salary of employees who either joined the company after
1986 or clerks earning below thousand
Sol : -
select ename,job,to_char(hiredate,'dd-mm-yyyy') hiredate,sal from emp
where to_char(hiredate,'yyyy') >=1986 or (sal<=1000 and job='CLERK')
ENAME JOB HIREDATE SAL
SMITH CLERK 17-12-1980 800
JAMES CLERK 03-12-1981 950
2. Display empname, job, hiredate, salary of employees who joined before 1987 and their
job is ANALYST or sal below 1000
Sol : -
select ename,job, to_char(hiredate,'dd-mm-yyyy') hiredate,sal from emp
where to_char(hiredate,'yyyy') <=1987 and job='ANALYST' or SAL <=1000
ENAME JOB HIREDATE SAL
SMITH CLERK 17-12-1980 800
SCOTT ANALYST 09-12-1982 3000
JAMES CLERK 03-12-1981 950
FORD ANALYST 03-12-1981 3000
3. Display empname, job, hiredate, salary of employees who earn above 1450 and should
be working in Dept no 10 or whose job is SALESMAN
Sol : -
select ename, deptno,job,hiredate,sal from emp
where sal>=1450 and deptno=10 or job='SALESMAN' order by deptno
ENAME DEPTNO JOB HIREDATE SAL
KING 10 PRESIDENT 17-NOV-81 5000
CLARK 10 MANAGER 09-JUN-81 2450
TURNER 30 SALESMAN 08-SEP-81 1500
WARD 30 SALESMAN 22-FEB-81 1250
ALLEN 30 SALESMAN 20-FEB-81 1600
MARTIN 30 SALESMAN 28-SEP-81 1250
4. Display empname, job, hiredate, salary of employees who work in dept. no 10 or job is
SALESMAN and earning above 1450
Sol : -
select ename,deptno,job,to_char(hiredate,'dd-MONTH-yyyy') hiredate,sal from emp
where deptno=10 or job='SALESMAN' and sal>=1450 order by deptno
ENAME DEPTNO JOB HIREDATE SAL
KING 10 PRESIDENT 17-NOVEMBER -1981 5000
MILLER 10 CLERK 23-JANUARY -1982 1300
CLARK 10 MANAGER 09-JUNE -1981 2450
TURNER 30 SALESMAN 08-SEPTEMBER-1981 1500
ALLEN 30 SALESMAN 20-FEBRUARY -1981 1600
Display empname, job, hiredate, salary of employees who are in Grade 1, 3 and 4
Sol : -
select ename,job,hiredate,sal from emp
where sal NOT between 1000 and 1999
ENAME JOB HIREDATE SAL
SMITH CLERK 17-DEC-80 800
JONES MANAGER 02-APR-81 2975
BLAKE MANAGER 01-MAY-81 2850
CLARK MANAGER 09-JUN-81 2450
SCOTT ANALYST 09-DEC-82 3000
KING PRESIDENT 17-NOV-81 5000
JAMES CLERK 03-DEC-81 950
FORD ANALYST 03-DEC-81 3000
7. Display dept no and total amount spent for salaries in each dept
Sol : -
select deptno,sum(sal) Total_Amount from emp
group by deptno order by deptno
DEPTNO TOTAL_AMOUNT
10 8750
20 10875
30 9400
8. Display job and total amount spent for salaries in each job category
Sol : -
select job,sum(sal) Total_Amount from emp
group by job order by job
JOB TOTAL_AMOUNT
ANALYST 6000
CLERK 4150
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600
9. Display dept no, job and total amount spent for salaries in each dept, job categoriwise
Sol : -
select deptno,job,sum(sal) Total_Amount from emp
group by deptno,job order by deptno,job
DEPTNO JOB TOTAL_AMOUNT
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
10.Display the dept no, employee name in small letters whose names start with S or J
Sol : -
select deptno,lower(ename) ename from emp
where upper(ename) like 'S%' or
upper(ename) like 'J%'
DEPTNO ENAME
20 smith
20 jones
20 scott
30 james
11.Display job, employee name of employees having 3rd letter as ‘A’ and find their length
Sol : -
select job,ename,length(ename) from emp
where ename like '__A%'
12.Display the no. of employees earning above 2000 in all departments except 10
Sol : -
select deptno,sal,count(sal) from emp
group by deptno,sal
having deptno!=10 and sal >=2000
14.Display the details of managers whose new salary per annum is above 40, 000 from
highest to lowest
Sol : -
select empno,ename,job,sal SALARY,(sal*12+(sal*12*25)/100) NEWSAL_PER_ANNUM
from emp
where job='MANAGER' and (sal*12+(sal*12*25)/100) > 40000
order by NEWSAL_PER_ANNUM desc
EMPNO ENAME JOB SALARY NEWSAL_PER_ANNUM
MANAGE
7566 JONES 2975 44625
R
MANAGE
7698 BLAKE 2850 42750
R
15.Update the salary of all the employees with new sal for dept no 10 only
Sol : -
update emp
set sal = (sal*12+(sal*12*25)/100)/12
where deptno=10
16.Display dept no, total salary whose total salary is greater 9000
Sol : -
select deptno,sum(sal) Total_Salary from emp
group by deptno
order by deptno
DEPTNO TOTAL_SALARY
10 8750
20 10875
30 9400
EMPLOYEE_NAME SALARY
KING 5000
FORD 3000
SCOTT 3000
27.Display the list of employees who have joined the company before 30-JUN-90 or after
31-DEC-90.
Sol : -
30.Display details of employees who joined in 1981 and drawing less than 3000 and
working for dept 20
Sol : -
select * from emp
where to_char(hiredate,'yyyy') =1981 and
sal <= 3000 and deptno = 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566 JONES MANAGER 7839 02-APR-81 2975 - 20
7902 FORD ANALYST 7566 03-DEC-81 3000 - 20
Sub Queries
31. Display ename, job, hiredate of RESAERCH dept.
Sol : -
select ename,job,hiredate from emp
where deptno in ( select deptno from dept where dname = 'RESEARCH')
ENAME JOB HIREDATE
FORD ANALYST 03-DEC-81
ADAMS CLERK 12-JAN-83
SCOTT ANALYST 09-DEC-82
JONES MANAGER 02-APR-81
SMITH CLERK 17-DEC-80
32.Display ename, job, hiredate of employees who work in CHICAGO .
Sol : -
select ename,job,hiredate from emp
where deptno in ( select deptno from dept where loc = 'CHICAGO')
ENAME JOB HIREDATE
JAMES CLERK 03-DEC-81
TURNER SALESMAN 08-SEP-81
BLAKE MANAGER 01-MAY-81
MARTIN SALESMAN 28-SEP-81
WARD SALESMAN 22-FEB-81
ALLEN SALESMAN 20-FEB-81
33.Display the ename, salary and deptno of the top 3 earners in each dept
Sol : -
select deptno,dname,sal, emp_sal_rank
from (select d.deptno,dname,ename,sal,
rank() over(partition by dname order by nvl(sal,0) desc) as emp_sal_rank
from dept d, emp e where d.deptno=e.deptno)
where emp_sal_rank <=3
DEPTNO DNAME SAL EMP_SAL_RANK
10 ACCOUNTING 5000 1
10 ACCOUNTING 2450 2
10 ACCOUNTING 1300 3
20 RESEARCH 3000 1
20 RESEARCH 3000 1
20 RESEARCH 2975 3
30 SALES 2850 1
30 SALES 1600 2
30 SALES 1500 3
Joins
36.Display employee name dept no and dept names
Sol : -
select e.ename,e.deptno,d.dname from emp e,dept d
where e.deptno=d.deptno
order by e.deptno
37.Display employee names and their department location with location in alphabetical
order
Sol : -
select e.ename,d.loc from emp e,dept d
where e.deptno=d.deptno
order by d.loc
ENAME LOC
BLAKE CHICAGO
TURNER CHICAGO
ALLEN CHICAGO
MARTIN CHICAGO
WARD CHICAGO
JAMES CHICAGO
SCOTT DALLAS
JONES DALLAS
SMITH DALLAS
ADAMS DALLAS
FORD DALLAS
KING NEW YORK
MILLER NEW YORK
CLARK NEW YORK
39. Display dept no , dept name, manager name for all departments
Sol : -
select e.deptno,d.dname Department_Name,e.ename MANAGER_NAME from emp e,dept d
where e.deptno=d.deptno and job='MANAGER'
order by e.deptno
DEPTNO DEPARTMENT_NAME MANAGER_NAME
10 ACCOUNTING CLARK
20 RESEARCH JONES
30 SALES BLAKE
40.Display the employee name and his manager name job wise
Sol : -
select b.job JOB, b.ename EMPLOYEE_NAME,a.ename MANAGER_NAME
from emp a,emp b
where b.mgr=a.empno
order by a.job