Advertisement
brandblox

Assignemnt 5

Nov 29th, 2023
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.39 KB | None | 0 0
  1. --TABLE 1:
  2.  
  3. CREATE TABLE EMPLOYEE (
  4.     EMPNO NUMBER(4) PRIMARY KEY,
  5.     EMPNAME VARCHAR2(50),
  6.     JOB VARCHAR2(30),
  7.     DEPTNO NUMBER(2),
  8.     SALARY NUMBER(8, 2),
  9.     COMM NUMBER(8, 2),
  10.     DOB DATE,
  11.     MNO NUMBER(4),
  12.     HIRE_DATE DATE
  13. );
  14.  
  15. --TABLE 2:
  16.  
  17. CREATE TABLE DEPTT (
  18.     DPTNO NUMBER(2) PRIMARY KEY,
  19.     DPTNAME VARCHAR2(50),
  20.     LOCATION VARCHAR2(50)
  21. );
  22.  
  23.  
  24.  
  25. --INSERT DATA:
  26.  
  27. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  28. VALUES (1, 'Oliver', ''Analyst', 1, 60000, 1000, TO_DATE('1989-01-10', 'yyyy-mm-dd'), 3, TO_DATE('2009-01-05', 'yyyy-mm-dd'));
  29.  
  30. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  31. VALUES (2, 'Samuel', 'Salesperson', 2, 25000, 150, TO_DATE('1995-09-21', 'yyyy-mm-dd'), 1, TO_DATE('2013-02-15', 'yyyy-mm-dd'));
  32.  
  33. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  34. VALUES (3, 'Alexander', 'Manager', 3, 50000, NULL, TO_DATE('1980-02-15', 'yyyy-mm-dd'), NULL, TO_DATE('2008-10-09', 'yyyy-mm-dd'));
  35.  
  36. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  37. VALUES (4, 'James', 'Analyst', 1, 40000, 5000, TO_DATE('1990-02-19', 'yyyy-mm-dd'), 1, TO_DATE('2016-05-01', 'yyyy-mm-dd'));
  38.  
  39. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  40. VALUES (5, Andrew', 'Salesperson', 2, 28000, 400, TO_DATE('1987-11-20', 'yyyy-mm-dd'), 1, TO_DATE('2016-05-10', 'yyyy-mm-dd'));
  41.  
  42. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  43. VALUES (6, 'Jennifer', 'Engineer', 3, 50000, 800, TO_DATE('1990-09-30', 'yyyy-mm-dd'), 2, TO_DATE('2015-12-05', 'yyyy-mm-dd'));
  44.  
  45. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  46. VALUES (7, 'Charles', 'Engineer', 4, 70000, 2500, TO_DATE('1987-02-09', 'yyyy-mm-dd'), 2, TO_DATE('2006-09-12', 'yyyy-mm-dd'));
  47.  
  48. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  49. VALUES (8, 'Anthony', 'Analyst', 1, 52000, 1500, TO_DATE('1995-09-06', 'yyyy-mm-dd'), 3, TO_DATE('2019-03-14', 'yyyy-mm-dd'));
  50.  
  51. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  52. VALUES (9, Joseph', 'Manager', 3, 68000, NULL, TO_DATE('1990-11-25', 'yyyy-mm-dd'), NULL, TO_DATE('2020-01-09', 'yyyy-mm-dd'));
  53.  
  54. INSERT INTO EMPLOYEE (EMPNO, EMPNAME, JOB, DEPTNO, SALARY, COMM, DOB, MNO, HIRE_DATE)
  55. VALUES (10, 'Isaac', 'Salesperson', 2, 26000, 400, TO_DATE('1996-02-25', 'yyyy-mm-dd'), 1, TO_DATE('2019-09-30', 'yyyy-mm-dd'));
  56.  
  57.  
  58. --sql query
  59.  
  60.  
  61. --Add a column "Marital Status" to the EMPLOYEE table
  62.  
  63. ALTER TABLE EMPLOYEE
  64. ADD Marital_Status VARCHAR2(20) CHECK (Marital_Status IN ('Unmarried', 'Married', 'Divorce'));
  65.  
  66. --Change the column name from MNO to MGRNO
  67.  
  68. ALTER TABLE EMPLOYEE
  69. RENAME COLUMN MNO TO MGRNO;
  70.  
  71. --Create a view named EMPVIEW containing EMPNO, EMPNAME, and SALARY of employees who work in department 3
  72.  
  73. CREATE VIEW EMPVIEW AS
  74. SELECT EMPNO, EMPNAME, SALARY
  75. FROM EMPLOYEE
  76. WHERE DEPTNO = 3;
  77.  
  78. --Create a view named deptt_summary that consists of deptt_name, no. of employees, total_salary, maximum salary, and minimum salary for each department
  79.  
  80. CREATE VIEW deptt_summary AS
  81. SELECT
  82.    D.DEPTNO AS Department_Number,
  83.    D.DEPTNAME AS Department_Name,
  84.    COUNT(E.EMPNO) AS No_of_Employees,
  85.    SUM(E.SALARY) AS Total_Salary,
  86.    MAX(E.SALARY) AS Maximum_Salary,
  87.    MIN(E.SALARY) AS Minimum_Salary
  88. FROM
  89.    DEPARTMENT D
  90. LEFT JOIN
  91.    EMPLOYEE E
  92. ON
  93.    D.DEPTNO = E.DEPTNO
  94. GROUP BY
  95.    D.DEPTNO, D.DEPTNAME;
  96.  
  97. --Grant INSERT and UPDATE privileges on DEPT to both RAM and SHAYM
  98.  
  99. GRANT INSERT, UPDATE ON DEPARTMENT TO RAM, SHAYM;
  100.  
  101. --Delete all information about department no. 3 and job='Engineer'
  102.  
  103. DELETE FROM EMPLOYEE
  104. WHERE DEPTNO = 3 AND JOB = 'Engineer';
  105.  
  106. --Modify the data type of DPTNO from number to varchar2
  107.  
  108. ALTER TABLE EMPLOYEE
  109. MODIFY (DEPTNO varchar2);
  110.  
  111. --Increase the salary of each employee by 10%
  112.  
  113. UPDATE EMPLOYEE
  114. SET SALARY = SALARY * 1.10;
  115.  
  116. --Show the total salary of each employee (including SAL and COMM)
  117.  
  118. Show the total salary of each employee (including SAL and COMM)
  119.  
  120. --Show the salary of each employee who has not received any commission
  121.  
  122. SELECT EMPNO, EMPNAME, SALARY
  123. FROM EMPLOYEE
  124. WHERE COMM IS NULL;
  125.  
  126. --Increase the salary of each employee as the sum of salary and comm
  127.  
  128. UPDATE EMPLOYEE
  129. SET SALARY = SALARY + COALESCE(COMM, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement