Advertisement
brandblox

Assignemnt 4

Oct 6th, 2023 (edited)
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.44 KB | None | 0 0
  1. -- Creating DEPTT table
  2. CREATE TABLE DEPTT (
  3.     DPTNO NUMBER PRIMARY KEY,
  4.     DPTNAME VARCHAR2(50),
  5.     LOCATION VARCHAR2(50)
  6. );
  7.  
  8. -- Creating EMPLOYEE table with foreign key reference to DEPTT table
  9. CREATE TABLE EMPLOYEE (
  10.     EMPNO NUMBER PRIMARY KEY,
  11.     EMPNAME VARCHAR2(50),
  12.     JOB VARCHAR2(50),
  13.     DEPTNO NUMBER,
  14.     SALARY NUMBER(10, 2),
  15.     COMM NUMBER(10, 2),
  16.     DOB DATE,
  17.     MANAGER_NO NUMBER,
  18.     HIRE_DATE DATE,
  19.     CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPTNO) REFERENCES DEPTT(DPTNO)
  20. );
  21.  
  22. --Display all employees whose names have 'AM' or 'SH' in them.
  23. SELECT EMPNAME, JOB, SALARY
  24. FROM EMPLOYEE
  25. WHERE EMPNAME LIKE '%AM%' OR EMPNAME LIKE '%SH%';
  26.  
  27. --List EMPNAME, JOB, and SALARY for all who have a manager.
  28. SELECT EMPNAME, JOB, SALARY
  29. FROM EMPLOYEE
  30. WHERE MANAGER_NO IS NOT NULL;
  31.  
  32. --Show EMPNAME and TOTAL remuneration of all employees.
  33. SELECT EMPNAME, SALARY + NVL(COMM, 0) AS TOTAL_REMUNERATION
  34. FROM EMPLOYEE;
  35.  
  36. --Show all employees who were hired during 1998.
  37. SELECT EMPNAME
  38. FROM EMPLOYEE
  39. WHERE EXTRACT(YEAR FROM HIRE_DATE) = 1998;
  40.  
  41. --Show name, annual salary, and commission of all sales people whose monthly salary is greater than their commission. The output should be ordered by name within salary, highest first.
  42. SELECT EMPNAME, SALARY * 12 AS ANNUAL_SALARY, COMM
  43. FROM EMPLOYEE
  44. WHERE JOB = 'Salesman' AND SALARY > NVL(COMM, 0)
  45. ORDER BY EMPNAME, SALARY DESC;
  46.  
  47. --Show the employee details under the heading “who, what and when”.
  48. SELECT EMPNAME || ' HAS HELD THE POSITION OF ' || JOB || ' IN DEPT ' || DEPTNO || ' SINCE ' || TO_CHAR(HIRE_DATE, 'DD-MON-YYYY') AS "who,what and when"
  49. FROM EMPLOYEE;
  50.  
  51. --Calculate and show the minimum salary for each different job.
  52. SELECT JOB, MIN(SALARY) AS MIN_SALARY
  53. FROM EMPLOYEE
  54. GROUP BY JOB;
  55.  
  56. --Show the average salary for job excluding manager.
  57. SELECT JOB, AVG(SALARY) AS AVG_SALARY
  58. FROM EMPLOYEE
  59. WHERE JOB <> 'Manager'
  60. GROUP BY JOB;
  61.  
  62. --Show the average monthly salary bill for each job type within a department.
  63. SELECT DEPTNO, JOB, AVG(SALARY) * 12 AS AVG_MONTHLY_SALARY_BILL
  64. FROM EMPLOYEE
  65. GROUP BY DEPTNO, JOB;
  66.  
  67. --Show the average salary for all departments employing more than three people.
  68. SELECT DEPTNO, AVG(SALARY) AS AVG_SALARY
  69. FROM EMPLOYEE
  70. GROUP BY DEPTNO
  71. HAVING COUNT(*) > 3;
  72.  
  73. --Show only those jobs where the maximum salary is greater than or equal to 1000.
  74. SELECT DISTINCT JOB
  75. FROM EMPLOYEE
  76. WHERE JOB IN (SELECT JOB FROM EMPLOYEE GROUP BY JOB HAVING MAX(SALARY) >= 1000);
  77.  
  78. --Show job and average salary for all excluding those of managers.
  79. SELECT JOB, AVG(SALARY) AS AVERAGE_SALARY
  80. FROM EMPLOYEE
  81. WHERE JOB <> 'Manager'
  82. GROUP BY JOB;
  83.  
  84. --Find all departments, which have more than 3 employees.
  85. SELECT DEPTNO
  86. FROM EMPLOYEE
  87. GROUP BY DEPTNO
  88. HAVING COUNT(*) > 3;
  89.  
  90. --Check whether all employee numbers are indeed unique.
  91. SELECT CASE
  92.          WHEN COUNT(EMPNO) = COUNT(DISTINCT EMPNO) THEN 'All Employee Numbers are Unique'
  93.          ELSE 'Duplicate Employee Numbers Exist'
  94.        END AS RESULT
  95. FROM EMPLOYEE;
  96.  
  97. --List managers’ minimum salary by excluding any groups where the minimum salary is less than 1000 and sort the output by salary.
  98. SELECT MANAGER_NO, MIN(SALARY) AS MINIMUM_SALARY
  99. FROM EMPLOYEE
  100. WHERE MANAGER_NO IS NOT NULL
  101. GROUP BY MANAGER_NO
  102. HAVING MIN(SALARY) >= 1000
  103. ORDER BY MINIMUM_SALARY;
  104.  
  105. --people working in HR department
  106. SELECT E.EMPNO, E.EMPNAME
  107. FROM EMPLOYEE029 E
  108. JOIN DEPTT D ON E.DEPTNO = D.DPTNO
  109. WHERE D.DPTNAME = 'HR Department';
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement