23
Day 1 1. List all the information about the employees in the EMP table. QUERY : select * from emp; OUTPUT: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ---------- --------- ---------- ---------- ------------ 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 2. List all the information about the departments in the DEPT table. QUERY : select * from dept; OUTPUT: DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 3. List the employees number,name,job title and hiredate of employees. QUERY : select empno, ename, job, hiredate from emp; OUTPUT: EMPNO ENAME JOB HIREDATE -------- ---------- --------- --------- 7369 SMITH CLERK 17-DEC-80 7499 ALLEN SALESMAN 20-FEB-81

Dbms Complete File (1)

Embed Size (px)

DESCRIPTION

database management system practical queries

Citation preview

Page 1: Dbms Complete File (1)

Day 1 1. List all the information about the employees in the EMP table. QUERY : select * from emp; OUTPUT: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ---------- --------- ---------- ---------- ------------ 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 2. List all the information about the departments in the DEPT table. QUERY : select * from dept; OUTPUT: DEPTNO DNAME LOC

---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 3. List the employees number,name,job title and hiredate of employees. QUERY : select empno, ename, job, hiredate from emp; OUTPUT: EMPNO ENAME JOB HIREDATE -------- ---------- --------- --------- 7369 SMITH CLERK 17-DEC-80 7499 ALLEN SALESMAN 20-FEB-81

Page 2: Dbms Complete File (1)

7521 WARD SALESMAN 22-FEB-81 7566 JONES MANAGER 02-APR-81 7654 MARTIN SALESMAN 28-SEP-81 7698 BLAKE MANAGER 01-MAY-81 7782 CLARK MANAGER 09-JUN-81 7788 SCOTT ANALYST 19-APR-87 7839 KING PRESIDENT 17-NOV-81 7844 TURNER SALESMAN 08-SEP-81 7876 ADAMS CLERK 23-MAY-87 7900 JAMES CLERK 03-DEC-81 7902 FORD ANALYST 03-DEC-81 7934 MILLER CLERK 23-JAN-82 4. Display name,job,salary,annual salary of all employees. QUERY : select ename, job, sal,sal*12 from emp; OUTPUT: ENAME JOB SAL SAL*12 ---------- --------- ---------- ---------- SMITH CLERK 800 9600 ALLEN SALESMAN 1600 19200 WARD SALESMAN 1250 15000 JONES MANAGER 2975 35700 MARTIN SALESMAN 1250 15000 BLAKE MANAGER 2850 34200 CLARK MANAGER 2450 29400 SCOTT ANALYST 3000 36000 KING PRESIDENT 5000 60000 TURNER SALESMAN 1500 18000 ADAMS CLERK 1100 13200 JAMES CLERK 950 11400 FORD ANALYST 3000 36000 MILLER CLERK 1300 15600 5. List contents of salgrade table. QUERY : select * from salgrade; OUTPUT: GRADE LOSAL HISAL ------ ---------- ---------- 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999

Page 3: Dbms Complete File (1)

6. Display all the different job types. QUERY : select distinct job from emp; OUTPUT: JOB --------- CLERK SALESMAN PRESIDENT MANAGER 7. Select the name and salary of all employees who are CLERK. QUERY : SELECT ENAME, SAL from EMP WHERE JOB='CLERK'; OUTPUT: ENAME SAL ---------- ---------- SMITH 800 ADAMS 1100 JAMES 950 MILLER 1300 8. List the employees number, name, job title, salary and hiredate of employees of

department number 20. QUERY : SELECT EMPNO, ENAME, JOB, SAL, HIREDATE FROM EMP WHERE DEPTNO=20; OUTPUT: EMPNO ENAME JOB SAL HIREDATE ---------- ---------- --------- ---------- --------- 7369 SMITH CLERK 800 17-DEC-80 7566 JONES MANAGER 2975 02-APR-81 7788 SCOTT ANALYST 3000 19-APR-87 7876 ADAMS CLERK 1100 23-MAY-87 7902 FORD ANALYST 3000 03-DEC-81 9. List the name, job title and salary of everyone hired on December 17, 1980. QUERY : SELECT ENAME, JOB, SAL FROM EMP WHERE HIREDATE= '17-DEC-80'; OUTPUT: ENAME JOB SAL ---------- --------- ---------- SMITH CLERK 800

Page 4: Dbms Complete File (1)

10. List the department name and department number for departments with numbers greater than or equal to 20.

QUERY : select dname, deptno from dept where deptno>=20; OUTPUT: DNAME DEPTNO -------------- ---------- RESEARCH 20 SALES 30 OPERATIONS 40 11. Select the name, salary and commission of employees whose commission is

greater than their salary. QUERY : select ename, sal,comm from emp where comm>sal; OUTPUT: ENAME SAL COMM ---------- ---------- ---------- MARTIN 1250 1400 12. List the names of employees where salary is less than 2500. QUERY : select ename from emp where sal<2500; OUTPUT: ENAME ---------- SMITH ALLEN WARD MARTIN CLARK TURNER ADAMS JAMES MILLER 13. List the names and employee number of managers who earn more than 2600.

Display in alphatical order by name. QUERY : select ename, empno from emp where job='MANAGER' AND SAL>2600 ORDER BY ename; OUTPUT: ENAME EMPNO ---------- ---------- BLAKE 7698 JONES 7566

Page 5: Dbms Complete File (1)

14. Select the information about manager and the president from the column job in

the EMP table. Order the resume by department number. QUERY : select * from emp where job IN('MANAGER','PRESIDENT') ORDER BY DEPTNO; OUTPUT: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- ---------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

Page 6: Dbms Complete File (1)

DAY 2 1. List all the employees names that do not end in ‘S’. QUERY : SELECT ENAME FROM EMP WHERE ENAME NOT LIKE '%S'; OUTPUT: ENAME ---------- SMITH ALLEN WARD MARTIN BLAKE CLARK SCOTT KING TURNER FORD MILLER

2. List the employees names that start with ‘C’. QUERY : SELECT ENAME FROM EMP WHERE ENAME LIKE 'C%'; OUTPUT: ENAME ---------- CLARK

3. List the name,job and department of everyone whose name falls in the alphabetical range ‘C’ to ‘L’.

QUERY : SELECT ENAME, JOB, DEPTNO FROM EMP WHERE ENAME BETWEEN 'C%' AND 'L%'; OUTPUT: ENAME JOB DEPTNO ---------- --------- ---------- JONES MANAGER 20 CLARK MANAGER 10 KING PRESIDENT 10 JAMES CLERK 30 FORD ANALYST 20

4. List employee details working in department 20, 30 or 40. QUERY : SELECT * FROM EMP WHERE DEPTNO IN(20,30,40);

Page 7: Dbms Complete File (1)

OUTPUT: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ---------- --------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20

5. List the employees while names start with ‘T’ and ends with ‘R’. QUERY : SELECT ENAME FROM EMP WHERE ENAME LIKE 'T%R'; OUTPUT: ENAME ---------- TURNER

6. Display all employee names which have ‘TH’ or ‘LL’ in them. QUERY : SELECT ENAME FROM EMP WHERE ENAME LIKE '%TH%' OR ENAME LIKE '%LL%'; OUTPUT: ENAME ---------- SMITH ALLEN MILLER

7. Display all employees who are hired during 1987. QUERY : SELECT * FROM EMP WHERE HIREDATE LIKE '%87%'; OUTPUT: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ---------- --------- ---------- --------- ---------- ---------- ---------- 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 8,9,10,11,12. Display the data as shown below for all employees. Who, what and when QUERY : SELECT ENAME || ' HAS HELD THE POSITION OF '|| JOB||' IN DEPT '||DEPTNO||' SINCE '||HIREDATE FROM EMP;

Page 8: Dbms Complete File (1)

OUTPUT: SMITH HAS HELD THE POSITION OF CLERK IN DEPT 20 SINCE 17-DEC-80 ALLEN HAS HELD THE POSITION OF SALESMAN IN DEPT 30 SINCE 20-FEB-81 WARD HAS HELD THE POSITION OF SALESMAN IN DEPT 30 SINCE 22-FEB-81 JONES HAS HELD THE POSITION OF MANAGER IN DEPT 20 SINCE 02-APR-81 MARTIN HAS HELD THE POSITION OF SALESMAN IN DEPT 30 SINCE 28-SEP-81 BLAKE HAS HELD THE POSITION OF MANAGER IN DEPT 30 SINCE 01-MAY-81 CLARK HAS HELD THE POSITION OF MANAGER IN DEPT 10 SINCE 09-JUN-81 SCOTT HAS HELD THE POSITION OF ANALYST IN DEPT 20 SINCE 19-APR-87 KING HAS HELD THE POSITION OF PRESIDENT IN DEPT 10 SINCE 17-NOV-81 TURNER HAS HELD THE POSITION OF SALESMAN IN DEPT 30 SINCE 08-SEP-81 ADAMS HAS HELD THE POSITION OF CLERK IN DEPT 20 SINCE 23-MAY-87 JAMES HAS HELD THE POSITION OF CLERK IN DEPT 30 SINCE 03-DEC-81 FORD HAS HELD THE POSITION OF ANALYST IN DEPT 20 SINCE 03-DEC-81 MILLER HAS HELD THE POSITION OF CLERK IN DEPT 10 SINCE 23-JAN-82 13. List the details of the employees in department10 and 20 in alphatical order of names. QUERY : SELECT * FROM EMP WHERE DEPTNO IN(10,20) ORDER BY ENAME; OUTPUT: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ---------- --------- ---------- ---------- ---------- 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 14. List all row form EMP table, by converting the NULL value in COMM column to ZERO(use NVL command). QUERY : SELECT NVL(COMM,0) FROM EMP; OUTPUT: NVL(COMM,0) ----------- 0 300 500 0 1400 0 0 0 0

Page 9: Dbms Complete File (1)

0 0 0 0 0 15. List all manager and salesman with salaries over 1500/-. QUERY : SELECT ENAME FROM EMP WHERE JOB IN('MANAGER','SALESMAN') AND SAL>1500; OUTPUT: ENAME ---------- ALLEN JONES BLAKE CLARK 16. Write a query that will accept a given job title and displays all records according

to that title. QUERY : SELECT * FROM EMP WHERE JOB = '&JOB_TITLE'; OUTPUT: Enter value for JOB_TITLE: MANAGER old 1: SELECT * FROM EMP WHERE JOB = '& JOB_TITLE ' new 1: SELECT * FROM EMP WHERE JOB = 'MANAGER' EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7566 JONES MANAGER 7839 02-APR-81 2975 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 17. List of employees who do not get any commission. QUERY : SELECT ENAME FROM EMP WHERE COMM IS NULL; OUTPUT: ENAME ---------- SMITH JONES BLAKE CLARK SCOTT KING ADAMS JAMES FORD MILLER

Page 10: Dbms Complete File (1)

DAY 3

1. List the names and hiredates of the employees in department 20. Display

the hiredate formatted as ’12/03/84’.

QUERY : select ename, to_char(hiredate,'dd/mm/yy') from emp where deptno=20; OUTPUT: ENAME TO_CHAR( ---------- -------- SMITH 17/12/80 JONES 02/04/81 SCOTT 19/04/87 ADAMS 23/05/87 FORD 03/12/81

2. How many months has the president worked for the company? Round to the nearest whole number of months.

QUERY : SELECT ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE)) FROM EMP WHERE JOB='PRESIDENT'; OUTPUT: ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE)) ----- 358

3. List the names of all employees whose hiredate is in the month of

December (use substr()function).

QUERY : select ename from emp where substr(HIREDATE,4,3)='DEC'; OUTPUT: ENAME -------------- SMITH JAMES FORD

4. Give SQL command to find the average annual salary per job in each department.

QUERY : select job,avg(sal*12) from emp group by job; OUTPUT: JOB AVG(SAL*12) --------- ----------- CLERK 12450 SALESMAN 16800 PRESIDENT 60000

Page 11: Dbms Complete File (1)

MANAGER 33100 ANALYST 36000

5. Count the number of people in department 30. QUERY : select count(*) from emp where deptno=30; OUTPUT: COUNT(*) ---------- 6

6. Compute the average, minimum and maximum slsries of employee for each department.

QUERY : select deptno, avg(sal),min(sal),max(sal) from emp group by deptno; OUTPUT: DEPTNO AVG(SAL) MIN(SAL) MAX(SAL) ---------- ---------- ---------- ---------- 30 1566.66667 950 2850 20 2175 800 3000

10 2916.66667 1300 5000

7. Display the deptno’s where morethan two clerks are working.

QUERY : SELECT DEPTNO FROM EMP WHERE JOB='CLERK' GROUP BY DEPTNO HAVING

COUNT(*)>2;

OUTPUT: no rows selected

8, 9. Produce the following output:

EMPLOYEE

SMITH(CLERK)

ALLEN(SALESMAN)

QUERY : SELECT ENAME || '(' || JOB || ')' FROM EMP; OUTPUT: ENAME||'('||JOB||')' --------------------- SMITH(CLERK) ALLEN(SALESMAN)

Page 12: Dbms Complete File (1)

WARD(SALESMAN) JONES(MANAGER) MARTIN(SALESMAN) BLAKE(MANAGER) CLARK(MANAGER) SCOTT(ANALYST) KING(PRESIDENT) TURNER(SALESMAN) ADAMS(CLERK) JAMES(CLERK) FORD(ANALYST) MILLER(CLERK)

10. Who was the first employee hired in each department.

QUERY : SELECT ENAME FROM EMP WHERE HIREDATE IN(SELECT MIN(HIREDATE) FROM EMP GROUP BY DEPTNO ; OUTPUT: ENAME -------------- ALLEN SMITH CLARK

11. Create a view consisting of employees and their total sum of salary dept wise. QUERY : create view emp1 as select ename,sal from emp order by deptno;

12. How many employee works in New York?

QUERY : SELECT COUNT(*) FROM EMP WHERE DEPTNO IN(SELECT DEPTNO FROM DEPT

WHERE LOC='NEW YORK')

OUTPUT: COUNT(*) --------------- 3

Page 13: Dbms Complete File (1)

13. Write a query to display as following:

ENAME HIRE_DATE ----------- ----------------- SMITH DECEMBER,SEVENTEEN 1980 ALLEN FEBRUARY,TWENTY 1981 WARD FEBRUARY,TWENTY TWO 1981 QUERY : select ename,to_char(hiredate,’month,ddsp,yyyy’) as HIRE_DATE from emp; OUTPUT: ENAME HIRE_DATE ----------- ----------------- SMITH DECEMBER,SEVENTEEN 1980 ALLEN FEBRUARY,TWENTY 1981 WARD FEBRUARY,TWENTY TWO 1981 JONES APRIL,TWO 1981 MARTIN SEPTEMBER, TWENTY EIGHT1981 BLAKE MAY,ONE 1981 CLARK JUNE,NINE 1981 SCOTT APRIL,NINETEEN 1987 KING NOVEMBER,SEVENTEEN 1981 TURNER SEPTEMBER,EIGHT 1981 ADAMS MAY, TWENTY THREE 1987 JAMES DECEMBER, THREE 1981 FORD DECEMBER,THREE 1981 MILLER JANUARY, TWENTY TWO 1982

14. Print a list of employees display just salary if more than 1500 if exactry 1500 display ‘on target’ if less than 1500 display ‘below 1500’.

QUERY : select ename,(case when sal>1500 then sal when sal=1500 then 'on target' when sal<1500 then 'below 1500' end)as salary from emp;

15. Determine the average salary of employees.

QUERY : SELECT AVG(SAL) FROM EMP;

OUTPUT:

AVG(SAL)

----------

2073.21429

16. List department number, department name , location local commission paid

and total salary of each department .

Page 14: Dbms Complete File (1)

QUERY : SELECT DEPT.DEPTNO, DEPT.DNAME, DEPT.LOC,EMP.COMM,EMP.SAL FROM DEPT

LEFT JOIN EMP ON DEPT.DEPTNO=EMP.DEPTNO;

OUTPUT: DEPTNO DNAME LOC COMM SAL ---------- -------------- ------------- ---------- ---------- 20 RESEARCH DALLAS 800 30 SALES CHICAGO 300 1600 30 SALES CHICAGO 500 1250 20 RESEARCH DALLAS 2975 30 SALES CHICAGO 1400 1250 30 SALES CHICAGO 2850 10 ACCOUNTING NEW YORK 2450 20 RESEARCH DALLAS 3000 10 ACCOUNTING NEW YORK 5000 30 SALES CHICAGO 0 1500 20 RESEARCH DALLAS 1100 30 SALES CHICAGO 950 20 RESEARCH DALLAS 3000 10 ACCOUNTING NEW YORK 1300

40 PERATIONS BOSTON

17. Display the average monthly salary bill for each job type within a department.

QUERY : select job, deptno, avg(sal) from emp group by job,deptno; OUTPUT: JOB DEPTNO AVG(SAL) --------- ---------- ---------- MANAGER 20 35700 PRESIDENT 10 60000 CLERK 10 15600 SALESMAN 30 16800 ANALYST 20 36000 MANAGER 30 34200 MANAGER 10 29400 CLERK 30 11400 CLERK 20 11400

18. To display only those jobs where the minimum salary is greater than or equal

to 3000.

QUERY : select distinct job from emp where sal>=3000;

OUTPUT:

JOB

---------

PRESIDENT

ANALYST

Page 15: Dbms Complete File (1)

19. Find the average salary and average total remuneration for each job type remember salesman earn commission.

QUERY : SELECT AVG(SAL), AVG((SAL+COMM)*12), JOB FROM EMP GROUP BY JOB; OUTPUT: AVG(SAL) AVG((SAL+COMM)*12) JOB ---------- ------------------ -------- 1037.5 CLERK 1400 23400 SALESMAN 5000 PRESIDENT 2758.33333 MANAGER

3000 ANALYST

20. Find out the difference between highest and lowest salaries.

QUERY : SELECT MAX(SAL)-MIN(SAL) FROM EMP; OUTPUT: MAX(SAL)-MIN(SAL) ----------------- 4200

21. Find all departments which have more than 3 employees.

QUERY : SELECT DNAME FROM DEPT WHERE DEPTNO IN( SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING COUNT(*)>3); OUTPUT: DNAME -------------- SALES RESEARCH

22. To display only those jobs where the maximum salary is greater than or equal to 3000.

QUERY : select distinct job from emp where (sal+NVL(comm,0))>=3000; OUTPUT: JOB ------------ PRESIDENT ANALYST

Page 16: Dbms Complete File (1)

23. List lowest paid employee working for each manager , exclude any group where the minimum salary is less than 1000 sort the out by salary.

QUERY : select manager,min(sal) from (select m.ename as manager,e.sal from emp m join emp e on e.mgr=m.empno) group by manager having min(sal)>1000 order by 2; OUTPUT: MANAGER MIN(SAL) ---------- ---------- FORD 9600 BLAKE 11400 SCOTT 13200 CLARK 15600 KING 29400 JONES 36000

Page 17: Dbms Complete File (1)

DAY- 4

1. Display all employees name and their department name in department name

order.

QUERY : SELECT EMP.ENAME, DEPT.DNAME FROM EMP LEFT JOIN DEPT ON DEPT.DEPTNO=EMP.DEPTNO ORDER BY DNAME; OUTPUT: ENAME DNAME --------- ------------------------------- CLARK ACCOUNTING KING ACCOUNTING MILLER ACCOUNTING SCOTT RESEARCH ADAMS RESEARCH FORD RESEARCH JONES RESEARCH SMITH RESEARCH JAMES SALES TURNER SALES BLAKE SALES MARTIN SALES WARD SALES ALLEN SALES

2. Display all employee name, department number and name.

QUERY : SELECT EMP.ENAME, EMP.DEPTNO, DEPT.DNAME FROM EMP LEFT JOIN DEPT ON DEPT.DEPTNO=EMP.DEPTNO OUTPUT: ENAME DEPTNO DNAME --------- -------------- ---------------- MILLER 10 ACCOUNTING KING 10 ACCOUNTING CLARK 10 ACCOUNTING FORD 20 RESEARCH ADAMS 20 RESEARCH SCOTT 20 RESEARCH JONES 20 RESEARCH SMITH 20 RESEARCH JAMES 30 SALES TURNER 30 SALES BLAKE 30 SALES MARTIN 30 SALES WARD 30 SALES

Page 18: Dbms Complete File (1)

3. Display the department that has no employee.

QUERY : select deptno from dept where deptno not in(select deptno from emp); OUTPUT: DEPTNO ---------- 40

4. Find all employees who joined the company before their manager.

QUERY : select e.ename as employee, m.ename as manager from emp m JOIN emp e ON e.mgr = m.empno where e.hiredate <m.hiredate; OUTPUT: EMPLOYEE MANAGER ------------- --------------- WARD BLAKE ALLEN BLAKE CLARK KING BLAKE KING JONES KING SMITH FORD

5. Find the employees who earn more than the lowest salary in each department.

QUERY : select ename, deptno, sal from emp where sal in(select sal from emp where sal not in(select min (sal) from emp group by deptno)); OUTPUT: ENAME DEPTNO SAL ---------- ---------- ---------- ALLEN 30 19200 WARD 30 15000 JONES 20 35700 MARTIN 30 15000 BLAKE 30 34200 CLARK 10 29400 SCOTT 20 36000 KING 10 60000 TURNER 30 18000 ADAMS 20 13200 FORD 20 36000

Page 19: Dbms Complete File (1)

6. Display employee who earn more than the salary in department 30

QUERY : SELECT ENAME FROM EMP WHERE SAL>(SELECT MIN(SAL) FROM EMP WHERE DEPTNO=30); OUTPUT: ENAME -------------------------- ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS FORD MILLER

7. Find employees who earn more than every employee in department 30

QUERY : SELECT ENAME FROM EMP WHERE SAL>(SELECT MAX(SAL) FROM EMP WHERE DEPTNO=30); OUTPUT: ENAME ----------- JONES SCOTT KING FORD

8. Find the job with highest average salary

QUERY : select job from(select job, avg(sal), row_number() over(order by avg(sal) desc) rn from emp group by job) where rn<2; OUTPUT: JOB --------- PRESIDENT

Page 20: Dbms Complete File (1)

9. To display all employees who earn less than their managers.

QUERY : select e.ename as employee, m.ename as manager from emp m JOIN emp e ON e.mgr = m.empno where e.sal <m.sal; OUTPUT: EMPLOYEE MANAGER ---------------- ---------------------------- JAMES BLAKE TURNER BLAKE MARTIN BLAKE WARD BLAKE ALLEN BLAKE MILLER CLARK ADAMS SCOTT CLARK KING BLAKE KING JONES KING SMITH FORD

10. Display the name of job, hiredate for employees whose salary is greater than the highest salary in any SALES department

QUERY : SELECT JOB, HIREDATE FROM EMP WHERE SAL>(SELECT MAX(EMP.SAL) FROM DEPT LEFT OUTER JOIN EMP ON DEPT.DEPTNO= EMP.DEPTNO WHERE DEPT.DNAME='SALES'); OUTPUT: JOB HIREDATE ------------------ -------------------- MANAGER 02-APR-81 ANALYST 19-APR-87 PRESIDENT 17-NOV-81 ANALYST 03-DEC-81

11. Find out names of the employees salary 2000 and working under FORD,

BLAKE, and KING. .(use set operators)

QUERY : SELECT E.ENAME AS EMPLOYEE,M.ENAME AS MANAGER FROM EMP M JOIN EMP E ON E.MGR = M.EMPNO WHERE E.SAL>2000 AND M.ENAME IN('FORD','BLAKE','KING'); OUTPUT: EMPLOYEE MANAGER ---------------- ------------------ JONES KING BLAKE KING CLARK KING

Page 21: Dbms Complete File (1)

12. Find out all the job either in department 20 or where salary is greater than

3000. (use set operators)

QUERY : SELECT JOB FROM EMP WHERE DEPTNO=20 UNION SELECT JOB FROM EMP WHERE SAL>3000; OUTPUT: JOB -------------- ANALYST CLERK MANAGER PRESIDENT

Page 22: Dbms Complete File (1)

DAY-5

1. Find out the employees who the highest salary in each department.

QUERY: SELECT ENAME FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO; OUTPUT: ENAME -------------------- BLAKE FORD SCOTT KING

2. Display the employees who are doing the same job as ‘FORD’.

QUERY: SELECT ENAME FROM EMP WHERE JOB = (SELECT JOB FROM EMP WHERE ENAME='FORD'); OUTPUT: ENAME ------------ SCOTT FORD

3. List the employees name and minimum salary earned by employee in each

department.

QUERY: SELECT ENAME,DEPTNO,SAL FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO); OUTPUT: ENAME DEPTNO SAL ---------- ----------- ------ JAMES 30 950 SMITH 20 800 MILLER 10 1300

4. List the top three earners in the company. Display their names and salary. QUERY: SELECT ENAME,SAL FROM (SELECT ename ,sal,ROW_NUMBER() OVER(ORDER BY sal DESC) rn FROM emp) WHERE rn < 4; OUTPUT: ENAME SAL -------- ------- KING 5000 SCOTT 3000 FORD 3000

Page 23: Dbms Complete File (1)

5. Display the employee working in TURNER’S department

QUERY: SELECT ENAME FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='TURNER'); OUTPUT: ENAME ------------------------ ALLEN WARD MARTIN BLAKE TURNER JAMES