Dbms 2

326 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
326
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dbms 2

  1. 1. DBMS CYCLE IICREATE TABLE JOB( CODE NUMBER(3) NOT NULL, NAME VARCHAR2(10) NOT NULL);ALTER TABLE JOB ADD PRIMARY KEY (CODE);CREATE TABLE LOCATION( CODE NUMBER(3) NOT NULL, NAME VARCHAR2(10) NOT NULL);ALTER TABLE LOCATION ADD PRIMARY KEY (CODE);CREATE TABLE DEPT( DEPTNO NUMBER(2) NOT NULL, NAME VARCHAR2(10), LOCATION NUMBER(3) NOT NULL);ALTER TABLE DEPT ADD PRIMARY KEY (DEPTNO);ALTER TABLE DEPT ADD FOREIGN KEY (LOCATION) REFERENCES LOCATION (CODE);CREATE TABLE EMPLOYEE( EMPNO NUMBER(4) NOT NULL, ENAME VARCHAR2(10), JOB NUMBER(3), MGR NUMBER(3), HIREDATE DATE, SAL NUMBER(8,2), COMM NUMBER(8,2), DEPTNO NUMBER(2) NOT NULL);ALTER TABLE EMPLOYEE ADD PRIMARY KEY (EMPNO);ALTER TABLE EMPLOYEE ADD FOREIGN KEY (JOB) REFERENCES JOB (CODE);
  2. 2. ALTER TABLE EMPLOYEE ADD FOREIGN KEY (MGR) REFERENCES EMPLOYEE (EMPNO);ALTER TABLE EMPLOYEE ADD FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO);SQL> DESC EMPLOYEE;Name Type Nullable Default Comments-------- ------------ -------- ------- --------EMPNO NUMBER(4)ENAME VARCHAR2(10) YJOB NUMBER(3) YMGR NUMBER(3) YHIREDATE DATE YSAL NUMBER(8,2) YCOMM NUMBER(8,2) YDEPTNO NUMBER(2)SQL> DESC DEPT;Name Type Nullable Default Comments-------- ------------ -------- ------- --------DEPTNO NUMBER(2)NAME VARCHAR2(10) YLOCATION NUMBER(3)SQL> DESC JOB;Name Type Nullable Default Comments---- ------------ -------- ------- --------CODE NUMBER(3)NAME VARCHAR2(10)SQL> DESC LOCATION;Name Type Nullable Default Comments---- ------------ -------- ------- --------CODE NUMBER(3)NAME VARCHAR2(10)SQL> DESC JOB_GRADES;Name Type Nullable Default Comments-------- --------- -------- ------- --------GRADE CHAR(1) YLOW_SAL NUMBER(6) YHIGH_SAL NUMBER(6) Y
  3. 3. SQL> SELECT * FROM EMPLOYEE ORDER BY EMPNO;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO----- ---------- ---- ---- ----------- ---------- ---------- ----------------------------------------- 101 ASHISH 1 105 4/10/1998 220000.00 10 102 SAGAR 1 106 4/10/1998 280000.00 20 103 WILLIAMS 2 10/1/1996 480000.00 40 104 GEORGE 3 10/1/1996 520000.00 40 105 PRADEEP 3 101 11/5/1999 250000.00 00.00 30 106 SRINIVAS 3 105 12/6/1999 300000.00 2000.00 30 107 KEERTHI 1 106 6/15/1998 1500.00 2000.00 10 108 CHAWLA 1 107 6/16/1998 1800.00 4000.00 10 109 SATIBABU 4 106 6/17/1998 12000.00 4000.00 20 110 SRINIVAS 4 106 6/18/1998 14000.00 4000.00 20 111 SURYA 3 122 4/10/1998 33000.00 600.00 10 112 SHYAM 3 122 4/14/1998 28000.00 700.00 20 113 DENNIS 3 121 6/22/1999 20000.00 1000.00 10 114 SUNIL 3 125 6/21/1999 16000.00 10 115 ANIL 4 103 4/22/1998 22000.00 28000.00 10 116 SUHAS 3 126 3/20/1999 18000.00 22000.00 20 117 SURAJ 3 127 1/21/1995 25000.00 30 118 LAKSHMI 3 128 2/22/1996 36000.00 30 119 KARTHIK 3 122 2/23/1996 16000.00 40 120 PULLARAO 3 122 2/24/1996 35000.00 40 121 SUSHMA 4 122 2/25/1996 36000.00 30 122 ASHA 4 122 2/26/1996 37000.00 30 123 SUNNELA 4 122 2/27/1996 29000.00 40 124 SPOORTHY 4 103 3/12/1998 17000.00 40 125 SUNAINA 3 103 3/13/1998 16000.00 10 126 DURGARAO 3 105 3/14/1998 38000.00 2000.00 40 127 PAVAN 3 122 3/15/1998 19000.00 3000.00 40 128 HEMANTH 4 103 3/16/1998 28000.00 4000.00 40 129 NARESH 4 103 3/17/1998 30000.00 5000.00 4029 rows selectedSQL> SELECT * FROM DEPT ORDER BY DEPTNO;DEPTNO NAME LOCATION------ ---------- ------------------------- 10 SALES 1 20 RESEARCH 3 30 ACCOUNTING 4 40 ADMIN 2
  4. 4. SQL> SELECT * FROM JOB ORDER BY CODE;CODE NAME---- -------------- 1 TRAINEE 2 PRESIDENT 3 ANALYST 4 CLERKSQL> SELECT * FROM LOCATION ORDER BY CODE;CODE NAME---- --------------- 1 VIJAYAWADA 2 KAKINADA 3 TIRUPATHI 4 CHICAGOSQL> SELECT * FROM JOB_GRADES ORDER BY 1;GRADE LOW_SAL HIGH_SAL----- ------- ----------------------------A 1000 10000B 10001 60000C 60001 150000D 150001 600000
  5. 5. EXCERCISE - VI1. List of departments with at least one ANALYSTSELECT DEPTNO, COUNT(EMPNO) "NO OF ANALYSTS" FROM EMPLOYEEWHERE JOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = ANALYST)GROUP BY DEPTNO HAVING COUNT(EMPNO) >= 1 ORDER BY DEPTNO;DEPTNO NO OF ANALYSTS------ ------------------------------- 10 4 20 2 30 4 40 54 rows selected2. Locations that have the SALES departmentSELECT * FROM LOCATION WHERE CODE IN (SELECT LOCATION FROM DEPT WHERE UPPER(NAME) =SALES);CODE NAME---- --------------- 1 VIJAYAWADA3. Locations that have at least one ANALYSTSELECT L.NAME, COUNT(E.EMPNO) "NO OF ANALYSTS" FROM EMPLOYEE E,DEPT D, JOB J, LOCATION L WHERE E.DEPTNO = D.DEPTNOAND D.LOCATION = L.CODE AND E.JOB = J.CODEGROUP BY L.NAME HAVING COUNT(E.EMPNO) >= 1;NAME NO OF ANALYSTS---------- -------------------------------TIRUPATHI 5VIJAYAWADA 8KAKINADA 10CHICAGO 64 rows selected4. Employees and their department namesSELECT E.EMPNO, E.ENAME, D.DEPTNO, D.NAME FROM EMPLOYEE E, DEPT D WHERE E.DEPTNO = D.DEPTNOORDER BY E.EMPNO;
  6. 6. EMPNO ENAME DEPTNO NAME----- ---------- ------ ----------------------------- 101 ASHISH 10 SALES 102 SAGAR 20 RESEARCH 103 WILLIAMS 40 ADMIN 104 GEORGE 40 ADMIN 105 PRADEEP 30 ACCOUNTING 106 SRINIVAS 30 ACCOUNTING 107 KEERTHI 10 SALES 108 CHAWLA 10 SALES 109 SATIBABU 20 RESEARCH 110 SRINIVAS 20 RESEARCH 111 SURYA 10 SALES 112 SHYAM 20 RESEARCH 113 DENNIS 10 SALES 114 SUNIL 10 SALES 115 ANIL 10 SALES 116 SUHAS 20 RESEARCH 117 SURAJ 30 ACCOUNTING 118 LAKSHMI 30 ACCOUNTING 119 KARTHIK 40 ADMIN 120 PULLARAO 40 ADMIN 121 SUSHMA 30 ACCOUNTING 122 ASHA 30 ACCOUNTING 123 SUNNELA 40 ADMIN 124 SPOORTHY 40 ADMIN 125 SUNAINA 10 SALES 126 DURGARAO 40 ADMIN 127 PAVAN 40 ADMIN 128 HEMANTH 40 ADMIN 129 NARESH 40 ADMIN29 rows selected5. List the employees who draw more than ‘DENNIS’ (using joins)SELECT E1.EMPNO, E1.ENAME, E1.SAL FROM EMPLOYEE E1, EMPLOYEE E2 WHERE UPPER(E2.ENAME) = DENNIS AND E1.SAL > E2.SAL;EMPNO ENAME SAL----- ---------- --------------------------- 101 ASHISH 220000.00 102 SAGAR 280000.00 103 WILLIAMS 480000.00 104 GEORGE 520000.00 105 PRADEEP 250000.00 106 SRINIVAS 300000.00 111 SURYA 33000.00 112 SHYAM 28000.00 115 ANIL 22000.00
  7. 7. 117 SURAJ 25000.00 118 LAKSHMI 36000.00 120 PULLARAO 35000.00 121 SUSHMA 36000.00 122 ASHA 37000.00 123 SUNNELA 29000.00 126 DURGARAO 38000.00 128 HEMANTH 28000.00 129 NARESH 30000.0018 rows selected6. Employees and their job functionsSELECT E.EMPNO, E.ENAME, J.CODE, J.NAME FROM EMPLOYEE E, JOB JWHERE E.JOB = J.CODE ORDER BY E.EMPNO;EMPNO ENAME CODE NAME----- ---------- ---- --------------------------------- 101 ASHISH 1 TRAINEE 102 SAGAR 1 TRAINEE 103 WILLIAMS 2 PRESIDENT 104 GEORGE 3 ANALYST 105 PRADEEP 3 ANALYST 106 SRINIVAS 3 ANALYST 107 KEERTHI 1 TRAINEE 108 CHAWLA 1 TRAINEE 109 SATIBABU 4 CLERK 110 SRINIVAS 4 CLERK 111 SURYA 3 ANALYST 112 SHYAM 3 ANALYST 113 DENNIS 3 ANALYST 114 SUNIL 3 ANALYST 115 ANIL 4 CLERK 116 SUHAS 3 ANALYST 117 SURAJ 3 ANALYST 118 LAKSHMI 3 ANALYST 119 KARTHIK 3 ANALYST 120 PULLARAO 3 ANALYST 121 SUSHMA 4 CLERK 122 ASHA 4 CLERK 123 SUNNELA 4 CLERK 124 SPOORTHY 4 CLERK 125 SUNAINA 3 ANALYST 126 DURGARAO 3 ANALYST 127 PAVAN 3 ANALYST 128 HEMANTH 4 CLERK 129 NARESH 4 CLERK29 rows selected
  8. 8. 7. Department name of DENNIS.SELECT E.ENAME, D.NAME FROM EMPLOYEE E, DEPT DWHERE E.DEPTNO = D.DEPTNO AND UPPER(E.ENAME) = DENNIS;ENAME NAME---------- ----------------DENNIS SALES8. Department name,Job name, and Location name of DENNISSELECT E.EMPNO, E.ENAME, D.NAME, L.NAME, J.NAME FROM EMPLOYEE E, DEPT D, LOCATION L, JOB JWHERE E.DEPTNO = D.DEPTNO AND E.JOB = J.CODE AND D.LOCATION = L.CODE AND UPPER(E.ENAME) = DENNIS;EMPNO ENAME NAME NAME NAME----- ---------- ----------------------------------------------- ---------- ---------- 113 DENNIS SALES VIJAYAWADA ANALYST
  9. 9. EXCERCISE - VII1. Employees working in SALES department (using Subquery or joins)SELECT E.EMPNO, E.ENAME, D.NAME FROM EMPLOYEE E, DEPT DWHERE E.DEPTNO = D.DEPTNO AND UPPER(D.NAME) = SALESORDER BY 1;EMPNO ENAME NAME----- ---------- --------------------- 101 ASHISH SALES 107 KEERTHI SALES 108 CHAWLA SALES 111 SURYA SALES 113 DENNIS SALES 114 SUNIL SALES 115 ANIL SALES 125 SUNAINA SALES8 rows selected2. List the department names and employees who draw commission less than 1000.Also display the departments even if no employee exists with the said condition.SELECT D.NAME, E.ENAME, E.COMM FROM (SELECT * FROM EMPLOYEE WHERE COMM < 1000) E, DEPT DWHERE E.DEPTNO(+) = D.DEPTNONAME ENAME COMM---------- ---------- ----------------------------------------RESEARCH SHYAM 700.00SALES SURYA 600.00ACCOUNTINGADMIN3. Employees and their locationsSELECT E.EMPNO, E.ENAME, L.CODE, L.NAME FROM EMPLOYEE E, DEPT D, LOCATION L WHERE E.DEPTNO = D.DEPTNO AND D.LOCATION = L.CODE ORDER BY 1;EMPNO ENAME CODE NAME----- ---------- ---- ---------------------------------------- 101 ASHISH 1 VIJAYAWADA 102 SAGAR 3 TIRUPATHI 103 WILLIAMS 2 KAKINADA 104 GEORGE 2 KAKINADA 105 PRADEEP 4 CHICAGO
  10. 10. 106 SRINIVAS 4 CHICAGO 107 KEERTHI 1 VIJAYAWADA 108 CHAWLA 1 VIJAYAWADA 109 SATIBABU 3 TIRUPATHI 110 SRINIVAS 3 TIRUPATHI 111 SURYA 1 VIJAYAWADA 112 SHYAM 3 TIRUPATHI 113 DENNIS 1 VIJAYAWADA 114 SUNIL 1 VIJAYAWADA 115 ANIL 1 VIJAYAWADA 116 SUHAS 3 TIRUPATHI 117 SURAJ 4 CHICAGO 118 LAKSHMI 4 CHICAGO 119 KARTHIK 2 KAKINADA 120 PULLARAO 2 KAKINADA 121 SUSHMA 4 CHICAGO 122 ASHA 4 CHICAGO 123 SUNNELA 2 KAKINADA 124 SPOORTHY 2 KAKINADA 125 SUNAINA 1 VIJAYAWADA 126 DURGARAO 2 KAKINADA 127 PAVAN 2 KAKINADA 128 HEMANTH 2 KAKINADA 129 NARESH 2 KAKINADA29 rows selected4. All the analysts and their locationsSELECT E.EMPNO, E.ENAME, J.NAME, L.NAME FROM EMPLOYEE E, DEPT D, LOCATION L, JOB JWHERE E.DEPTNO = D.DEPTNO AND D.LOCATION = L.CODE AND E.JOB = J.CODE AND UPPER(J.NAME) = ANALYSTORDER BY 1;EMPNO ENAME NAME NAME----- ---------- ---------- ------------------------------ 104 GEORGE ANALYST KAKINADA 105 PRADEEP ANALYST CHICAGO 106 SRINIVAS ANALYST CHICAGO 111 SURYA ANALYST VIJAYAWADA 112 SHYAM ANALYST TIRUPATHI 113 DENNIS ANALYST VIJAYAWADA 114 SUNIL ANALYST VIJAYAWADA 116 SUHAS ANALYST TIRUPATHI 117 SURAJ ANALYST CHICAGO 118 LAKSHMI ANALYST CHICAGO 119 KARTHIK ANALYST KAKINADA 120 PULLARAO ANALYST KAKINADA
  11. 11. 125 SUNAINA ANALYST VIJAYAWADA 126 DURGARAO ANALYST KAKINADA 127 PAVAN ANALYST KAKINADA15 rows selected5. List the employees who are earning maximum salary in their respectivedepartmentsSELECT EMPNO, ENAME, SAL, DEPTNO FROM EMPLOYEEWHERE (DEPTNO, SAL) IN (SELECT DEPTNO, MAX(SAL) FROM EMPLOYEE GROUP BY DEPTNO);EMPNO ENAME SAL DEPTNO----- ---------- ---------- ----------------------------- 106 SRINIVAS 300000.00 30 104 GEORGE 520000.00 40 102 SAGAR 280000.00 20 101 ASHISH 220000.00 106. List employees whose salary exceeds their managersSELECT OUTER.EMPNO, OUTER.ENAME, OUTER.SAL, OUTER.MGR FROM EMPLOYEE OUTER WHERE EXISTS(SELECT 1 FROM EMPLOYEE INNER WHERE INNER.EMPNO = OUTER.MGR AND INNER.SAL < OUTER.SAL);EMPNO ENAME SAL MGR----- ---------- ---------- -------------------------------- 108 CHAWLA 1800.00 107 106 SRINIVAS 300000.00 105 105 PRADEEP 250000.00 101 117 SURAJ 25000.00 127 118 LAKSHMI 36000.00 1287. Employees and their salary gradesSELECT E.EMPNO, E.ENAME, J.GRADE FROM EMPLOYEE E, JOB_GRADES JWHERE E.SAL BETWEEN J.LOW_SAL AND J.HIGH_SAL;EMPNO ENAME GRADE----- ---------- --------------------------- 107 KEERTHI A 108 CHAWLA A 109 SATIBABU B 110 SRINIVAS B 125 SUNAINA B 119 KARTHIK B
  12. 12. 114 SUNIL B 124 SPOORTHY B 116 SUHAS B 127 PAVAN B 113 DENNIS B 115 ANIL B 117 SURAJ B 112 SHYAM B 128 HEMANTH B 123 SUNNELA B 129 NARESH B 111 SURYA B 120 PULLARAO B 121 SUSHMA B 118 LAKSHMI B 122 ASHA B 126 DURGARAO B 101 ASHISH D 105 PRADEEP D 102 SAGAR D 106 SRINIVAS D 103 WILLIAMS D 104 GEORGE D29 rows selected
  13. 13. EXCERCISE - VIII1. List the average annual salary for all the employeesSELECT AVG(SAL) FROM EMPLOYEE;AVG(SAL)----------88906.89652. The maximum salary of clerks in the companySELECT J.NAME, MAX(SAL) "MAXIMUM_SALARY" FROM EMPLOYEE E, JOB J WHERE E.JOB = J.CODE AND UPPER(J.NAME) = CLERK GROUP BY J.NAME;NAME MAXIMUM_SALARY---------- ---------------------------------CLERK 370003. How many employees are drawing commission?SELECT COUNT(EMPNO) FROM EMPLOYEE WHERE COMM IS NOT NULL;COUNT(EMPNO)-------------------------- 154. Find all the ANALYSTS with the minimum salary among ANALYSTSSELECT EMPNO,ENAME,JOB,SAL FROM EMPLOYEEWHERE (JOB, SAL) = (SELECT JOB, MIN(SAL) FROM EMPLOYEE WHERE JOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = ANALYST) GROUP BY JOB);EMPNO ENAME JOB SAL----- ---------- ---- ---------------------------- 125 SUNAINA 3 16000.00 119 KARTHIK 3 16000.00 114 SUNIL 3 16000.00
  14. 14. 5. Find the total salary and commission for all the employeesSELECT EMPNO, ENAME, (SAL + NVL(COMM, 0)) "TOTAL_SALARY",NVL(COMM, 0) FROM EMPLOYEE;EMPNO ENAME TOTAL_SALARY NVL(COMM,0)----- ---------- ------------ -------------------------------------------- 125 SUNAINA 16000 0 124 SPOORTHY 17000 0 123 SUNNELA 29000 0 122 ASHA 37000 0 121 SUSHMA 36000 0 120 PULLARAO 35000 0 119 KARTHIK 16000 0 118 LAKSHMI 36000 0 117 SURAJ 25000 0 116 SUHAS 40000 22000 115 ANIL 50000 28000 114 SUNIL 16000 0 113 DENNIS 21000 1000 112 SHYAM 28700 700 111 SURYA 33600 600 110 SRINIVAS 18000 4000 109 SATIBABU 16000 4000 108 CHAWLA 5800 4000 107 KEERTHI 3500 2000 106 SRINIVAS 302000 2000 105 PRADEEP 252000 2000 104 GEORGE 520000 0 103 WILLIAMS 480000 0 102 SAGAR 280000 0 101 ASHISH 220000 0 126 DURGARAO 40000 2000 127 PAVAN 22000 3000 128 HEMANTH 32000 4000 129 NARESH 35000 500029 rows selected
  15. 15. 6. Find all the employees who earn more than the average salary of their respectivedepartmentsSELECT OUTER.EMPNO, OUTER.ENAME, OUTER.SAL, OUTER.DEPTNO FROM EMPLOYEEOUTER WHERE OUTER.SAL > (SELECT AVG(INNER.SAL) FROM EMPLOYEE INNER WHERE INNER.DEPTNO = OUTER.DEPTNO)ORDER BY 1;EMPNO ENAME SAL DEPTNO----- ---------- ---------- ------------------------- 101 ASHISH 220000.00 10 102 SAGAR 280000.00 20 103 WILLIAMS 480000.00 40 104 GEORGE 520000.00 40 105 PRADEEP 250000.00 30 106 SRINIVAS 300000.00 307. Find average salary, number of employees and total salary department wiseSELECT DEPTNO, AVG(SAL), COUNT(EMPNO), SUM(SAL) FROM EMPLOYEE GROUP BY DEPTNO ORDER BY 1;DEPTNO AVG(SAL) COUNT(EMPNO) SUM(SAL)------ ---------- ------------ -------------------------------------- 10 41287.5 8 330300 20 70400 5 352000 30 114000 6 684000 40 121200 10 12120008. Average salary of all analysts, department wiseSELECT DEPTNO, AVG(SAL) FROM EMPLOYEEWHERE JOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = ANALYST)GROUP BY DEPTNO ORDER BY 1;DEPTNO AVG(SAL)------ ---------- 10 21250 20 23000 30 152750 40 125600
  16. 16. 9. List the departments with at least two clerksSELECT DEPTNO, COUNT(EMPNO) "TOTAL_NO_OF_EMPLOYEES" FROM EMPLOYEEWHERE JOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = CLERK)GROUP BY DEPTNO HAVING COUNT(EMPNO) >= 2 ORDER BY 1;DEPTNO TOTAL_NO_OF_EMPLOYEES------ --------------------- 20 2 30 2 40 410.Number of employees and average salary for each department in each locationSELECT D.DEPTNO, D.NAME, L.CODE, L.NAME, COUNT(E.EMPNO), AVG(E.SAL) FROM EMPLOYEE E, DEPT D, LOCATION LWHERE E.DEPTNO = D.DEPTNO AND D.LOCATION = L.CODE GROUP BY D.DEPTNO, D.NAME, L.CODE,L.NAME ORDER BY 1;DEPTNO NAME CODE NAME COUNT(E.EMPNO) AVG(E.SAL)------ ---------- ---- ---------- -------------- ------------------------------------------------- 10 SALES 1 VIJAYAWADA 8 41287.5 20 RESEARCH 3 TIRUPATHI 5 70400 30 ACCOUNTING 4 CHICAGO 6 114000 40 ADMIN 2 KAKINADA 10 121200
  17. 17. EXCERCISE - IX1. Find the number of employees and average salary for each job group in eachdepartment having more than 3 employees in each job group.SELECT DEPTNO, JOB, COUNT(EMPNO), AVG(SAL) FROM EMPLOYEEGROUP BY DEPTNO, JOB HAVING COUNT(EMPNO) > 3ORDER BY 1;DEPTNO JOB COUNT(EMPNO) AVG(SAL)------ ---- ------------ ---------------------------------------- 10 3 4 21250 30 3 4 152750 40 3 5 125600 40 4 4 260002. Find the number of analysts in each department having more than one employeeSELECT DEPTNO, COUNT(EMPNO) "NO_OF_EMPLOYEES" FROM EMPLOYEEWHERE JOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = ANALYST)GROUP BY DEPTNO HAVING COUNT(EMPNO) > 0ORDER BY 1;DEPTNO NO_OF_EMPLOYEES------ --------------- 10 4 20 2 30 4 40 53. Display the names of employees along with their department names from thedepartments having at least three employeesSELECT E.EMPNO, E.ENAME, D.DEPTNO, D.NAME FROM EMPLOYEE E, DEPT DWHERE E.DEPTNO = D.DEPTNO AND E.DEPTNO IN (SELECT DEPTNO FROM EMPLOYEE GROUP BY DEPTNO HAVINGCOUNT(EMPNO) > 3) ORDER BY 1;EMPNO ENAME DEPTNO NAME----- ---------- ------ ------------------------------ 101 ASHISH 10 SALES 102 SAGAR 20 RESEARCH 103 WILLIAMS 40 ADMIN 104 GEORGE 40 ADMIN 105 PRADEEP 30 ACCOUNTING 106 SRINIVAS 30 ACCOUNTING 107 KEERTHI 10 SALES 108 CHAWLA 10 SALES
  18. 18. 109 SATIBABU 20 RESEARCH 110 SRINIVAS 20 RESEARCH 111 SURYA 10 SALES 112 SHYAM 20 RESEARCH 113 DENNIS 10 SALES 114 SUNIL 10 SALES 115 ANIL 10 SALES 116 SUHAS 20 RESEARCH 117 SURAJ 30 ACCOUNTING 118 LAKSHMI 30 ACCOUNTING 119 KARTHIK 40 ADMIN 120 PULLARAO 40 ADMIN 121 SUSHMA 30 ACCOUNTING 122 ASHA 30 ACCOUNTING 123 SUNNELA 40 ADMIN 124 SPOORTHY 40 ADMIN 125 SUNAINA 10 SALES 126 DURGARAO 40 ADMIN 127 PAVAN 40 ADMIN 128 HEMANTH 40 ADMIN 129 NARESH 40 ADMIN29 rows selected4. Name the departments where in you have atleast one employee drawing more thanthe average salary of their respective department.SELECT DEPTNO, NAME FROM DEPTWHERE DEPTNO IN (SELECT OUTER.DEPTNO FROM EMPLOYEE OUTER WHERE OUTER.SAL > (SELECT AVG(INNER.SAL) FROM EMPLOYEE INNER WHERE INNER.DEPTNO = OUTER.DEPTNO));DEPTNO NAME------ -------------------------- 10 SALES 20 RESEARCH 30 ACCOUNTING 40 ADMIN5. Find out all departments with at least one employeeSELECT DEPTNO, NAME FROM DEPTWHERE DEPTNO IN (SELECT DISTINCT DEPTNO FROM EMPLOYEE);DEPTNO NAME
  19. 19. ------ ----------------------- 10 SALES 20 RESEARCH 30 ACCOUNTING 40 ADMIN6. List of departments with at least one ANALYSTSELECT DEPTNO, NAME FROM DEPT WHERE DEPTNO IN (SELECT DISTINCT DEPTNO FROM EMPLOYEE WHEREJOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = ANALYST));DEPTNO NAME------ --------------------- 10 SALES 20 RESEARCH 30 ACCOUNTING 40 ADMIN7. Locations that have SALES departmentSELECT L.CODE, L.NAME FROM DEPT D, LOCATION LWHERE D.LOCATION = L.CODE AND UPPER(D.NAME) = SALES;CODE NAME---- ---------- 1 VIJAYAWADA8.Locations that have at least one ANALYSTSELECT DISTINCT L.CODE, L.NAME FROM EMPLOYEE E, DEPT D, LOCATION L, JOB JWHERE E.DEPTNO = D.DEPTNO AND E.JOB = J.CODE AND D.LOCATION = L.CODE AND UPPER(J.NAME) = ANALYST;CODE NAME---- ---------- 1 VIJAYAWADA 2 KAKINADA 3 TIRUPATHI 4 CHICAGO
  20. 20. EXCERCISE – X1.Employes with department namesSELECT E.EMPNO, E.ENAME, D.DEPTNO, D.NAME FROM EMPLOYEE E, DEPT DWHERE E.DEPTNO = D.DEPTNO ORDER BY 1;EMPNO ENAME DEPTNO NAME----- ---------- ------ ---------------------------- 101 ASHISH 10 SALES 102 SAGAR 20 RESEARCH 103 WILLIAMS 40 ADMIN 104 GEORGE 40 ADMIN 105 PRADEEP 30 ACCOUNTING 106 SRINIVAS 30 ACCOUNTING 107 KEERTHI 10 SALES 108 CHAWLA 10 SALES 109 SATIBABU 20 RESEARCH 110 SRINIVAS 20 RESEARCH 111 SURYA 10 SALES 112 SHYAM 20 RESEARCH 113 DENNIS 10 SALES 114 SUNIL 10 SALES 115 ANIL 10 SALES 116 SUHAS 20 RESEARCH 117 SURAJ 30 ACCOUNTING 118 LAKSHMI 30 ACCOUNTING 119 KARTHIK 40 ADMIN 120 PULLARAO 40 ADMIN 121 SUSHMA 30 ACCOUNTING 122 ASHA 30 ACCOUNTING 123 SUNNELA 40 ADMIN 124 SPOORTHY 40 ADMIN 125 SUNAINA 10 SALES 126 DURGARAO 40 ADMIN 127 PAVAN 40 ADMIN 128 HEMANTH 40 ADMIN 129 NARESH 40 ADMIN29 rows selected2.Employees and their job functionSELECT E.EMPNO, E.ENAME, E.JOB, J.NAME FROM EMPLOYEE E, JOB J WHERE E.JOB = J.CODE ORDER BY 1;EMPNO ENAME JOB NAME----- ---------- ---- -------------------------------- 101 ASHISH 1 TRAINEE 102 SAGAR 1 TRAINEE
  21. 21. 103 WILLIAMS 2 PRESIDENT 104 GEORGE 3 ANALYST 105 PRADEEP 3 ANALYST 106 SRINIVAS 3 ANALYST 107 KEERTHI 1 TRAINEE 108 CHAWLA 1 TRAINEE 109 SATIBABU 4 CLERK 110 SRINIVAS 4 CLERK 111 SURYA 3 ANALYST 112 SHYAM 3 ANALYST 113 DENNIS 3 ANALYST 114 SUNIL 3 ANALYST 115 ANIL 4 CLERK 116 SUHAS 3 ANALYST 117 SURAJ 3 ANALYST 118 LAKSHMI 3 ANALYST 119 KARTHIK 3 ANALYST 120 PULLARAO 3 ANALYST 121 SUSHMA 4 CLERK 122 ASHA 4 CLERK 123 SUNNELA 4 CLERK 124 SPOORTHY 4 CLERK 125 SUNAINA 3 ANALYST 126 DURGARAO 3 ANALYST 127 PAVAN 3 ANALYST 128 HEMANTH 4 CLERK 129 NARESH 4 CLERK29 rows selected3.Deparment name of the DennisSELECT DEPTNO, NAME FROM DEPTWHERE DEPTNO = (SELECT DEPTNO FROM EMPLOYEE WHEREUPPER(ENAME) = DENNIS);DEPTNO NAME------ -------------------------- 10 SALES4.Location,Department name,job name of DennisSELECT E.ENAME, D.NAME, L.NAME, J.NAME FROM EMPLOYEE E, DEPT D, JOB J, LOCATION LWHERE E.DEPTNO = D.DEPTNO AND D.LOCATION = L.CODE AND E.JOB = J.CODE AND UPPER(E.ENAME) = DENNIS;
  22. 22. ENAME NAME NAME NAME---------- ---------- ---------- -------------------------------DENNIS SALES VIJAYAWADA ANALYST5.Employees working in sales departmentSELECT E.EMPNO,E.ENAME,D.NAME FROM EMPLOYEE E,DEPT DWHERE E.DEPTNO=D.DEPTNO AND D.NAME=SALES ORDER BY 1;EMPNO ENAME NAME----- ---------- -------------------------- 101 ASHISH SALES 107 KEERTHI SALES 108 CHAWLA SALES 111 SURYA SALES 113 DENNIS SALES 114 SUNIL SALES 115 ANIL SALES 125 SUNAINA SALES6.Employees and their locationsSELECT E.EMPNO, E.ENAME, L.CODE, L.NAME FROM EMPLOYEE E, DEPT D, LOCATION LWHERE E.DEPTNO = D.DEPTNO AND D.LOCATION = L.CODEORDER BY 1;EMPNO ENAME CODE NAME----- ---------- ---- --------------------------------------- 101 ASHISH 1 VIJAYAWADA 102 SAGAR 3 TIRUPATHI 103 WILLIAMS 2 KAKINADA 104 GEORGE 2 KAKINADA 105 PRADEEP 4 CHICAGO 106 SRINIVAS 4 CHICAGO 107 KEERTHI 1 VIJAYAWADA 108 CHAWLA 1 VIJAYAWADA 109 SATIBABU 3 TIRUPATHI 110 SRINIVAS 3 TIRUPATHI 111 SURYA 1 VIJAYAWADA 112 SHYAM 3 TIRUPATHI 113 DENNIS 1 VIJAYAWADA 114 SUNIL 1 VIJAYAWADA 115 ANIL 1 VIJAYAWADA 116 SUHAS 3 TIRUPATHI 117 SURAJ 4 CHICAGO 118 LAKSHMI 4 CHICAGO 119 KARTHIK 2 KAKINADA 120 PULLARAO 2 KAKINADA 121 SUSHMA 4 CHICAGO
  23. 23. 122 ASHA 4 CHICAGO 123 SUNNELA 2 KAKINADA 124 SPOORTHY 2 KAKINADA 125 SUNAINA 1 VIJAYAWADA 126 DURGARAO 2 KAKINADA 127 PAVAN 2 KAKINADA 128 HEMANTH 2 KAKINADA 129 NARESH 2 KAKINADA29 rows selected7.All the ANALYSTS and their locationsSELECT E.EMPNO, E.ENAME, L.CODE, L.NAME FROM EMPLOYEE E, DEPT D, LOCATION LWHERE E.DEPTNO = D.DEPTNO AND D.LOCATION = L.CODE AND E.JOB = (SELECT CODE FROM JOB WHERE UPPER(NAME) = ANALYST)ORDER BY 1;EMPNO ENAME CODE NAME----- ---------- ---- -------------------------------- 104 GEORGE 2 KAKINADA 105 PRADEEP 4 CHICAGO 106 SRINIVAS 4 CHICAGO 111 SURYA 1 VIJAYAWADA 112 SHYAM 3 TIRUPATHI 113 DENNIS 1 VIJAYAWADA 114 SUNIL 1 VIJAYAWADA 116 SUHAS 3 TIRUPATHI 117 SURAJ 4 CHICAGO 118 LAKSHMI 4 CHICAGO 119 KARTHIK 2 KAKINADA 120 PULLARAO 2 KAKINADA 125 SUNAINA 1 VIJAYAWADA 126 DURGARAO 2 KAKINADA 127 PAVAN 2 KAKINADA15 rows selected8.List of all employees earn maximum salary in thier respective departmentsSELECT D.DEPTNO,D.NAME,E.EMPNO, E.ENAME, E.SAL FROM EMPLOYEE E,DEPT DWHERE (D.DEPTNO, E.SAL) IN
  24. 24. (SELECT DEPTNO, MAX(SAL) FROM EMPLOYEE GROUP BY DEPTNO);DEPTNO NAME EMPNO ENAME SAL------ ---------- ----- ---------- ----------------------------------------- 10 SALES 101 ASHISH 220000.00 20 RESEARCH 102 SAGAR 280000.00 30 ACCOUNTING 106 SRINIVAS 300000.00 40 ADMIN 104 GEORGE 520000.009.List of employees whos salary exceeds their managers.SELECT OUTER.EMPNO, OUTER.ENAME, OUTER.SAL FROM EMPLOYEEOUTER WHERE EXISTS (SELECT 1 FROM EMPLOYEE INNER WHERE INNER.EMPNO = OUTER.MGR AND INNER.SAL < OUTER.SAL) ORDER BY 1;EMPNO ENAME SAL----- ---------- -------------------------------- 105 PRADEEP 250000.00 106 SRINIVAS 300000.00 108 CHAWLA 1800.00 117 SURAJ 25000.00 118 LAKSHMI 36000.00

×