sql-queries-withSolutions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

SQL – Queries for DBMS Lab Record - 2013

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

5. Employee salaries in Employee are categorized as following grades


Grade Salary
1 500 - 999
2 1000 – 1999
3 2000 – 2999
4 3000 - 5000

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

6. Display dept no, minimum salary, maximum salary of each dept


Sol : -
select deptno,min(sal) MINIMUM_SALARY ,max(sal) MAXIMUM_SALARY from emp
group by deptno order by deptno
DEPTNO MINIMUM_SALARY MAXIMUM_SALARY
10 1300 5000
20 800 3000
30 950 2850

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%'

JOB ENAME LENGTH(ENAME)


MANAGER BLAKE 5
MANAGER CLARK 5
CLERK ADAMS 5

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

DEPTNO SAL COUNT(SAL)


20 2975 1
20 3000 2
30 2850 1
13.Display employee name, job, salary , new salary (25% increase annually) for salesman
only
Sol : -
select ename,job,sal SALARY,(sal*12+(sal*12*25)/100)/12 NEWSAL from emp
where job='SALESMAN'
ENAME JOB SALARY NEWSAL
ALLEN SALESMAN 1600 2000
WARD SALESMAN 1250 1562.5
MARTIN SALESMAN 1250 1562.5
TURNER SALESMAN 1500 1875

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

17.Display the average salary of each dept


Sol : -
select deptno,sum(sal) Average_Salary from emp
group by deptno
order by deptno
DEPTNO AVERAGE_SALARY
10 8750
20 10875
30 9400

18.Display the average salary of each job category


Sol : -
select job,sum(sal) Average_Salary from emp
group by job
order by job
JOB AVERAGE_SALARY
ANALYST 6000
CLERK 4150
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600

19.Display the average salary of each job category department wise


Sol : -
select deptno, job,sum(sal) Average_Salary from emp
group by deptno,job
order by deptno,job
DEPTNO JOB AVERAGE_SALARY
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

20.Display the details of employees department wise according to the seniority


Sol : -
select deptno,empno,ename,to_char(hiredate,'dd-mm-yyyy') hiredate from emp
order by deptno,hiredate

DEPTNO EMPNO ENAME HIREDATE


10 7782 CLARK 09-06-1981
10 7839 KING 17-11-1981
10 7934 MILLER 23-01-1982
20 7566 JONES 02-04-1981
20 7902 FORD 03-12-1981
20 7788 SCOTT 09-12-1982
20 7876 ADAMS 12-01-1983
20 7369 SMITH 17-12-1980
30 7698 BLAKE 01-05-1981
30 7900 JAMES 03-12-1981
30 7844 TURNER 08-09-1981
30 7499 ALLEN 20-02-1981
30 7521 WARD 22-02-1981
30 7654 MARTIN 28-09-1981

21.Display the names of the employees in alphabetical order department wise


Sol : -
select deptno,ename,empno,hiredate,sal from emp
order by deptno,ename
DEPTNO ENAME EMPNO HIREDATE SAL
10 CLARK 7782 09-JUN-81 2450
10 KING 7839 17-NOV-81 5000
10 MILLER 7934 23-JAN-82 1300
20 ADAMS 7876 12-JAN-83 1100
20 FORD 7902 03-DEC-81 3000
20 JONES 7566 02-APR-81 2975
20 SCOTT 7788 09-DEC-82 3000
20 SMITH 7369 17-DEC-80 800
30 ALLEN 7499 20-FEB-81 1600
30 BLAKE 7698 01-MAY-81 2850
30 JAMES 7900 03-DEC-81 950
30 MARTIN 7654 28-SEP-81 1250
30 TURNER 7844 08-SEP-81 1500
30 WARD 7521 22-FEB-81 1250

22.Determine the no. of employees in each dept.


Sol : -
select deptno,count(empno)NO_OF_EMPLOYEES from emp
group by deptno
order by deptno
DEPTNO NO_OF_EMPLOYEES
10 3
20 5
30 6

23.Determine the no. of employees in each job category


Sol : -
select job,count(empno)NO_OF_EMPLOYEES from emp
group by job
order by job
JOB NO_OF_EMPLOYEES
ANALYST 2
CLERK 4
MANAGER 3
PRESIDENT 1
SALESMAN 4

24.Determine the no. of employees in each dept. job category wise


Sol : -
select deptno, job,count(empno)NO_OF_EMPLOYEES from emp
group by deptno,job
order by deptno,job
DEPTNO JOB NO_OF_EMPLOYEES
10 CLERK 1
10 MANAGER 1
10 PRESIDENT 1
20 ANALYST 2
20 CLERK 2
20 MANAGER 1
30 CLERK 1
30 MANAGER 1
30 SALESMAN 4

25.Determine the no. of salesman and clerks


Sol : -
select job,count(empno) NO_OF_SALESMAN_AND_CLERKS from emp
group by job
having job='SALESMAN' or job='CLERK'
order by job
JOB NO_OF_SALESMAN_AND_CLERKS
CLERK 4
SALESMAN 4

26.Display the details of top 3 earners of the employee table


Sol : -
select ename EMPLOYEE_NAME ,sal SALARY from emp e
where 3 > ( select count(*) from emp where e.sal < sal)
order by sal desc

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 : -

28.Display ename, deptno, location of all clerks


Sol : -
select e.ename,e.deptno,d.loc from emp e, dept d
where e.deptno=d.deptno and e.job='CLERK'

ENAME DEPTNO LOC


MILLER 10 NEW YORK
ADAMS 20 DALLAS
SMITH 20 DALLAS
JAMES 30 CHICAGO

29.Find all departments which has more than 3 employees


Sol : -
select deptno, count(empno) NO_OF_EMPLOYEES from emp
group by deptno
having count(empno) >= 3
DEPTNO NO_OF_EMPLOYEES
30 6
20 5
10 3

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

34.Display the top earners in each department


Sol : -
select deptno,ename EMPLOYEE_NAME, sal SALARY from emp a
where a.sal = (select max(b.sal) from emp b where a.deptno = b.deptno)
order by a.deptno,a.sal desc
DEPTNO EMPLOYEE_NAME SALARY
10 KING 5000
20 SCOTT 3000
20 FORD 3000
30 BLAKE 2850

35. Display the details of senior most person in each dept.


Sol : -
select deptno,ename EMPLOYEE_NAME,to_char(a.hiredate,'dd-mm-yyyy') hiredate from
emp a
where hiredate = (select max(hiredate) from emp b where a.deptno = b.deptno)
order by a.deptno

DEPTNO EMPLOYEE_NAME HIREDATE


10 MILLER 23-01-1982
20 ADAMS 12-01-1983
30 JAMES 03-12-1981

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

ENAME DEPTNO DNAME


CLARK 10 ACCOUNTING
KING 10 ACCOUNTING
MILLER 10 ACCOUNTING
JONES 20 RESEARCH
FORD 20 RESEARCH
ADAMS 20 RESEARCH
SMITH 20 RESEARCH
SCOTT 20 RESEARCH
WARD 30 SALES
TURNER 30 SALES
ALLEN 30 SALES
JAMES 30 SALES
BLAKE 30 SALES
MARTIN 30 SALES

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

38.Determine the no. of employees working at same location


Sol : -
select d.loc LOCATION,count(e.empno) NO_OF_EMPLOYEES from emp e,dept d
where e.deptno=d.deptno
group by d.loc
LOCATION NO_OF_EMPLOYEES
NEW YORK 3
CHICAGO 6
DALLAS 5

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

JOB EMPLOYEE_NAME MANAGER_NAME


CLERK ADAMS SCOTT
CLERK SMITH FORD
CLERK JAMES BLAKE
SALESMAN TURNER BLAKE
SALESMAN MARTIN BLAKE
SALESMAN WARD BLAKE
CLERK MILLER CLARK
ANALYST SCOTT JONES
ANALYST FORD JONES
SALESMAN ALLEN BLAKE
MANAGER BLAKE KING
MANAGER JONES KING
MANAGER CLARK KING

You might also like