Advertisement
brandblox

Assignemnt 6

Nov 29th, 2023
1,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.32 KB | None | 0 0
  1. -- Create DEPTT table
  2. CREATE TABLE DEPTT (
  3.     DPTNO INT PRIMARY KEY,
  4.     DPTNAME VARCHAR(50),
  5.     LOCATION VARCHAR(50)
  6. );
  7.  
  8. -- Create SALGRADE table
  9. CREATE TABLE SALGRADE (
  10.     MINSAL INT,
  11.     MAXSAL INT,
  12.     GRADE INT PRIMARY KEY
  13. );
  14.  
  15. -- Create EMPLOYEE table with foreign key references
  16. CREATE TABLE EMPLOYEE (
  17.     EMPNO INT PRIMARY KEY,
  18.     EMPNAME VARCHAR(50),
  19.     JOB VARCHAR(50),
  20.     DEPTNO INT,
  21.     SALARY INT,
  22.     COMM INT,
  23.     DOB DATE,
  24.     MANAGER_NO INT,
  25.     HIRE_DATE DATE,
  26.     FOREIGN KEY (DEPTNO) REFERENCES DEPTT(DPTNO),
  27.     FOREIGN KEY (MANAGER_NO) REFERENCES EMPLOYEE(EMPNO),
  28.     CONSTRAINT CHK_SALARY CHECK (SALARY BETWEEN (SELECT MIN(MINSAL) FROM SALGRADE) AND (SELECT MAX(MAXSAL) FROM SALGRADE))
  29. );
  30.  
  31. --Show name, salary, and grade for all employees.
  32. SELECT E.EMPNAME, E.SALARY, S.GRADE
  33. FROM EMPLOYEE E
  34. JOIN SALGRADE S ON E.SALARY BETWEEN S.MINSAL AND S.MAXSAL;
  35.  
  36. --Show all employee names and their department name, in department name order.
  37. SELECT E.EMPNAME, D.DPTNAME
  38. FROM EMPLOYEE E
  39. JOIN DEPTT D ON E.DEPTNO = D.DPTNO
  40. ORDER BY D.DPTNAME;
  41.  
  42. --List employee details except clerks, sorted by descending salary
  43. SELECT E.EMPNAME, E.JOB, E.SALARY, S.GRADE, D.DPTNAME
  44. FROM EMPLOYEE E
  45. JOIN SALGRADE S ON E.SALARY BETWEEN S.MINSAL AND S.MAXSAL
  46. JOIN DEPTT D ON E.DEPTNO = D.DPTNO
  47. WHERE E.JOB != 'CLERK'
  48. ORDER BY E.SALARY DESC;
  49.  
  50. --List employees earning Rs. 84000/year or clerks
  51. SELECT E.EMPNAME, E.JOB, E.SALARY, E.DEPTNO, D.DPTNAME, S.GRADE
  52. FROM EMPLOYEE E
  53. JOIN SALGRADE S ON E.SALARY BETWEEN S.MINSAL AND S.MAXSAL
  54. JOIN DEPTT D ON E.DEPTNO = D.DPTNO
  55. WHERE E.SALARY = 84000 OR E.JOB = 'CLERK';
  56.  
  57. --Show distinct jobs in dept_no 20 or 25.
  58. SELECT DISTINCT JOB
  59. FROM EMPLOYEE
  60. WHERE DEPTNO IN (20, 25);
  61.  
  62. --Show common jobs of dept_no 20 and 25
  63. SELECT JOB
  64. FROM EMPLOYEE
  65. WHERE DEPTNO = 20
  66. INTERSECT
  67. SELECT JOB
  68. FROM EMPLOYEE
  69. WHERE DEPTNO = 25;
  70.  
  71. --Show jobs of dept_no 10 except those in dept_no 25
  72. SELECT JOB
  73. FROM EMPLOYEE
  74. WHERE DEPTNO = 10
  75. MINUS
  76. SELECT JOB
  77. FROM EMPLOYEE
  78. WHERE DEPTNO = 25;
  79.  
  80. --Show departments without employees
  81. SELECT D.DPTNAME
  82. FROM DEPTT D
  83. LEFT JOIN EMPLOYEE E ON D.DPTNO = E.DEPTNO
  84. WHERE E.EMPNO IS NULL;
  85.  
  86. --List employees with managers' names and numbers
  87. SELECT E1.EMPNAME AS EmployeeName, E1.EMPNO AS EmployeeNumber, E2.EMPNAME AS ManagerName, E2.EMPNO AS ManagerNumber
  88. FROM EMPLOYEE E1
  89. LEFT JOIN EMPLOYEE E2 ON E1.MANAGER_NO = E2.EMPNO;
  90.  
  91. --Show employees with no manager (assuming managers have NULL in MANAGER_NO field)
  92. SELECT EMPNAME
  93. FROM EMPLOYEE
  94. WHERE MANAGER_NO IS NULL;
  95.  
  96. --Find the job filled in the first half of 1985 and 1986
  97. SELECT JOB
  98. FROM EMPLOYEE
  99. WHERE EXTRACT(YEAR FROM HIRE_DATE) = 1985 AND EXTRACT(MONTH FROM HIRE_DATE) <= 6
  100. UNION
  101. SELECT JOB
  102. FROM EMPLOYEE
  103. WHERE EXTRACT(YEAR FROM HIRE_DATE) = 1986 AND EXTRACT(MONTH FROM HIRE_DATE) <= 6;
  104.  
  105. --Find employees who joined before their managers
  106. SELECT E1.EMPNAME AS EmployeeName, E2.EMPNAME AS ManagerName
  107. FROM EMPLOYEE E1
  108. JOIN EMPLOYEE E2 ON E1.MANAGER_NO = E2.EMPNO
  109. WHERE E1.HIRE_DATE < E2.HIRE_DATE;
  110.  
  111. --Employees with the lowest salary in each department
  112. SELECT E1.EMPNAME, E1.SALARY, E1.DEPTNO
  113. FROM EMPLOYEE E1
  114. WHERE E1.SALARY = (SELECT MIN(E2.SALARY) FROM EMPLOYEE E2 WHERE E1.DEPTNO = E2.DEPTNO);
  115.  
  116. --Employees with the lowest salary in their department
  117. SELECT E1.EMPNAME, E1.SALARY, E1.DEPTNO
  118. FROM EMPLOYEE E1
  119. WHERE E1.SALARY = (SELECT MIN(SALARY) FROM EMPLOYEE E2 WHERE E1.DEPTNO = E2.DEPTNO);
  120.  
  121. --Employees earning more than the lowest salary in department 20
  122. SELECT EMPNAME, SALARY
  123. FROM EMPLOYEE
  124. WHERE SALARY > (SELECT MIN(SALARY) FROM EMPLOYEE WHERE DEPTNO = 20);
  125.  
  126. --Employees earning more than every employee in department 20.
  127. SELECT EMPNAME, SALARY
  128. FROM EMPLOYEE
  129. WHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO = 20);
  130.  
  131. --Departments with an average salary bill greater than deptt-30.
  132. SELECT D.DPTNAME
  133. FROM DEPTT D
  134. JOIN EMPLOYEE E ON D.DPTNO = E.DEPTNO
  135. GROUP BY D.DPTNAME
  136. HAVING AVG(E.SALARY) > (SELECT AVG(SALARY) FROM EMPLOYEE WHERE DEPTNO = 30);
  137.  
  138. --Job with the highest average salary.
  139. SELECT JOB
  140. FROM EMPLOYEE
  141. GROUP BY JOB
  142. ORDER BY AVG(SALARY) DESC
  143. FETCH FIRST 1 ROWS ONLY;
  144.  
  145. --Employees with salary higher than the highest in any sales department
  146. SELECT EMPNAME, JOB, HIRE_DATE
  147. FROM EMPLOYEE
  148. WHERE SALARY > (SELECT MAX(SALARY) FROM EMPLOYEE WHERE DEPTNO IN (SELECT DPTNO FROM DEPTT WHERE DPTNAME = 'SALES'));
  149.  
  150. --Employees who don't manage anyone
  151. SELECT EMPNAME
  152. FROM EMPLOYEE
  153. WHERE EMPNO NOT IN (SELECT MANAGER_NO FROM EMPLOYEE WHERE MANAGER_NO IS NOT NULL);
  154.  
  155. --Most recently hired employees in each department
  156. SELECT EMPNAME, HIRE_DATE
  157. FROM (
  158.     SELECT EMPNAME, HIRE_DATE, DEPTNO,
  159.            ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY HIRE_DATE DESC) AS rnum
  160.     FROM EMPLOYEE
  161. ) WHERE rnum = 1;
  162.  
  163. --Departments with no employees (using subquery)
  164. SELECT DPTNAME
  165. FROM DEPTT
  166. WHERE DPTNO NOT IN (SELECT DISTINCT DEPTNO FROM EMPLOYEE);
  167.  
  168. --Top 5 earners in the company.
  169. SELECT EMPNAME, SALARY
  170. FROM EMPLOYEE
  171. ORDER BY SALARY DESC
  172. FETCH FIRST 5 ROWS ONLY;
  173.  
  174. --Departments with employees from all salary grades
  175. SELECT D.DPTNAME
  176. FROM DEPTT D
  177. WHERE NOT EXISTS (
  178.     SELECT DISTINCT GRADE
  179.     FROM SALGRADE S
  180.     WHERE NOT EXISTS (
  181.         SELECT 1
  182.         FROM EMPLOYEE E
  183.         WHERE E.DEPTNO = D.DPTNO AND E.SALARY BETWEEN S.MINSAL AND S.MAXSAL
  184.     )
  185. );
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement