Consider the following table structures related to a university for the below…

2020

 Consider the following table structures related to a university for the below question

Refer table, structures given above, University decided to give all employees in the ‘SCIENCE’ department a 20% rise in salary. Which of the following query/queries will compute the above results?

(A)    UPDATE EMPLOYEE

        SET SALARY = SALARY*1.20

        WHERE DEPT NO. IN (SELECT DID FROM DEPARTMENT WHERE DNAME = ‘SCIENCE’);

(B)   UPDATE TABLE EMPLOYEE

        SET SALARY = SALARY*1.20 WHERE DNAME=’SCIENCE’; 

(C)     ALTER TABLE EMPLOYEE

          SET SALARY=SALARY*1.20

          WHERE DEPTNO. IN (SELECT DNAME FROM DEPARTMENT WHERE DNAME = ‘SCEINCE’)

Choose the correct answer from the options given below:

  1. A.

    (A) and (B) Only

  2. B.

    (A) Only

  3. C.

    (B) and (C) Only

  4. D.

    (C) Only

Attempted by 283 students.

Show answer & explanation

Correct answer: B

Final answer: Only the UPDATE that multiplies SALARY for employees whose DEPTNO matches the DID of the 'SCIENCE' department is correct.

Why this works:

  • The statement "UPDATE EMPLOYEE SET SALARY = SALARY*1.20 WHERE DEPTNO IN (SELECT DID FROM DEPARTMENT WHERE DNAME = 'SCIENCE');" is valid because DEPTNO is a foreign key referencing the department's DID. The subquery returns the DID(s) for departments named 'SCIENCE', and the UPDATE updates only employees in those departments.

Why the other statements are incorrect or invalid:

  • A statement that uses "UPDATE TABLE EMPLOYEE SET SALARY = SALARY*1.20 WHERE DNAME = 'SCIENCE';" is invalid for two reasons: standard SQL uses "UPDATE EMPLOYEE ..." (not "UPDATE TABLE EMPLOYEE"), and DNAME is not a column in the EMPLOYEE table. To filter by department name you must either use a subquery on DID (as above) or join to the DEPARTMENT table in the UPDATE.

  • A statement that uses "ALTER TABLE EMPLOYEE SET SALARY = SALARY*1.20 ..." is invalid because ALTER TABLE modifies schema (columns/constraints), not row values. In addition, selecting the wrong column in the subquery or misspelling 'SCIENCE' would prevent correct identification of the target department.

Correct examples to achieve the raise:

  1. Using a subquery: UPDATE EMPLOYEE SET SALARY = SALARY * 1.20 WHERE DEPTNO IN (SELECT DID FROM DEPARTMENT WHERE DNAME = 'SCIENCE');

  2. Using a join (database dialects that support UPDATE ... FROM): UPDATE EMPLOYEE e SET SALARY = SALARY * 1.20 FROM DEPARTMENT d WHERE e.DEPTNO = d.DID AND d.DNAME = 'SCIENCE';

Conclusion: Only the UPDATE that uses DEPTNO matched to DID for the 'SCIENCE' department is correct. The other statements contain syntax or semantic errors and will not produce the intended result.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor