Comprehension: Consider the following table structures related to a university for given questions. EMPLOYEE NAME VARCHAR (30) NOT NULL, EID VARCHAR (10) NOT NULL, DEPTNO INT (5) NOT NULL, HODEID VARCHAR (10), SALARY INT (10), PRIMARY KEY (EID), FOREIGN KEY (HODEID) REFRENCES EMPLOYEE (EID), FOREIGN KEY (DEPTNO) REFRENCES DEPARTMENT (DID); DEPARTMENT DID INT (5) NOT NULL, DNAME VARCHAR (30) NOT NULL, HODID VARCHAR (10) NOT NULL, HODNAME VARCHAR (30), PRIMARY KEY (DID), UNIQUE (DNAME), FOREIGN KEY (HODID) REFERENCES EMPLOYEE (EID), PROJECT WORE: EMPID VARCHAR (10) NOT NULL, PROJNO INT (5) NOT NULL, PROJECTLOC VARCHAR (30) NOT NULL, PRIMARY KEY (EMPID, PROJNQ), FOREIGN KEY (EMPID) REFERENCES EMPLOYEE (EID),
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 ='SCIENCE’) Choose the correct answer from the options given below:
(A) only
The question asks us to find the correct SQL query to update the salary of employees belonging to the 'SCIENCE' department. To do this, we need to understand how the employee and department information is stored and linked in the database. Let's look at the structure of the relevant tables provided:
| Table Name | Columns | Constraints |
|---|---|---|
| EMPLOYEE | EMPLOYEENAME VARCHAR (30) NOT NULL, EID VARCHAR (10) NOT NULL, DEPTNO INT (5) NOT NULL, HODEID VARCHAR (10), SALARY INT (10) | PRIMARY KEY (EID), FOREIGN KEY (HODEID) REFERENCES EMPLOYEE (EID), FOREIGN KEY (DEPTNO) REFERENCES DEPARTMENT (DID) |
| DEPARTMENT | DID INT (5) NOT NULL, DNAME VARCHAR (30) NOT NULL, HODID VARCHAR (10) NOT NULL, HODNAME VARCHAR (30) | PRIMARY KEY (DID), UNIQUE (DNAME), FOREIGN KEY (HODID) REFERENCES EMPLOYEE (EID) |
| PROJECT WORE | EMPID VARCHAR (10) NOT NULL, PROJNO INT (5) NOT NULL, PROJECTLOC VARCHAR (30) NOT NULL | PRIMARY KEY (EMPID, PROJNQ), FOREIGN KEY (EMPID) REFERENCES EMPLOYEE (EID) |
From this schema, we can see that:
Our goal is to update the SALARY column in the EMPLOYEE table for those employees whose DEPTNO matches the DID of the department where DNAME is 'SCIENCE'. The salary needs to be increased by 20%, which means multiplying the current salary by 1.20 (Current Salary $\times$ 1.20).
Let's examine each provided query to see which one correctly performs this task.
Query (A) is:
UPDATE EMPLOYEE
SET SALARY = SALARY*1.20
WHERE DEPT NO. IN (SELECT DID FROM DEPARTMENT WHERE DNAME='SCIENCE');
Query (A) uses the appropriate command and logic to achieve the desired salary update based on the department name.
Query (B) is:
UPDATE TABLE EMPLOYEE
SET SALARY = SALARY*1.20
WHERE DNAME='SCIENCE’;
Query (B) has syntactic errors and incorrect logic for filtering employees by department name.
Query (C) is:
ALTER TABLE EMPLOYEE
SET SALARY=SALARY*1.20
WHERE DEPTNO. IN (SELECT DNAME FROM DEPARTMENT WHERE DNAME ='SCIENCE’)
Query (C) uses the wrong command and has logical errors in its filtering condition.
Comparing the three queries, only Query (A) correctly uses the UPDATE statement, targets the EMPLOYEE table, specifies the correct salary increase calculation, and employs a valid method (using a subquery with DEPTNO and DID) to identify the employees belonging to the 'SCIENCE' department based on the database schema.
| SQL Command | Primary Function | Example Use Case |
|---|---|---|
| SELECT | Retrieve data | Finding employees in a specific department. |
| INSERT | Add new data | Adding a new employee record. |
| UPDATE | Modify existing data | Changing an employee's salary or department. |
| DELETE | Remove data | Removing a retired employee's record. |
| ALTER TABLE | Modify table structure | Adding a new column for employee email. |
When you need to update or select data from one table based on a condition in another related table, you typically use joins or subqueries. Query (A) effectively uses a subquery to find the relevant department ID (DID) and then uses that ID to filter the employees based on their DEPTNO.
Another common way to perform updates based on related tables, depending on the specific SQL database system, is using a JOIN within the UPDATE statement. For instance, in some systems, you might write something similar to:
UPDATE EMPLOYEE E
SET E.SALARY = E.SALARY * 1.20
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.DEPTNO = D.DID
WHERE D.DNAME = 'SCIENCE';
However, the subquery method shown in Query (A) is widely supported and a valid approach for this task.
In SQL, _______ is an Aggregate function.
Match the following -
| List I | List II | ||
| (a) | DDL | (i) | LOCK TABLE |
| (b) | DML | (ii) | COMMIT |
| (c) | TCL | (iii) | Natural Difference |
| (d) | Binary operation | (iv) | REVOKE |
In SQL, which of the following command is used to modify a column in a table?
Which of the following keyword is used to eliminate duplicate records in SQL?
_______ SQL command changes one or more fields in a record.