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),
Which of the following query/queries return the employee ID and name of employees whose salary is greater than the salary of all employees in department number 20 of university. Order result by employee ID (refer table structures given above). (A) SELECT EID, NAME FROM EMPLOYEE WHERE SALARY > (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20) ORDER BY EID: (B) SELECT EID, NAME FROM EMPLOYEE WHERE SALARY > (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20); (C) SELECT EID, NAME FROM EMPLOYEE WHERE SALARY > ALL(SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20) ORDER BY EID Choose the correct answer from the options given below:
(C) only
The question asks us to find the employee ID and name of employees whose salary is strictly greater than the salary of all employees in department number 20. The result should be ordered by employee ID (EID).
We are given three potential SQL queries and need to determine which one(s correctly achieve this goal based on the provided database table structures, particularly the EMPLOYEE table which contains EID, EMPLOYEENAME, DEPTNO, and SALARY.
Let's examine each query:
The first query is:
SELECT EID, NAME FROM EMPLOYEE WHERE SALARY > (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20) ORDER BY EID;
This query uses a subquery (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20). This subquery returns a set of all salaries for employees in department 20. The outer query then attempts to compare an employee's SALARY with this entire set using the simple > operator.
However, due to the incorrect use of the > operator with a set to mean "greater than all", this query is likely incorrect.
The second query is:
SELECT EID, NAME FROM EMPLOYEE WHERE SALARY > (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20);
This query is very similar to query (A), using the same subquery and the simple > operator to compare a single salary against a set of salaries from department 20.
Therefore, this query is incorrect for two reasons: the improper comparison operator for the required logic and the missing ordering.
The third query is:
SELECT EID, NAME FROM EMPLOYEE WHERE SALARY > ALL(SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20) ORDER BY EID;
This query also uses the subquery (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO=20) to get the set of salaries for department 20 employees. However, it uses the > ALL operator.
This query correctly implements the required logic using the appropriate SQL operator and includes the necessary ordering.
Based on the analysis, only query (C) correctly uses the > ALL operator to ensure that an employee's salary is greater than every salary in department 20 and includes the required sorting by EID.
| Query | Correct Comparison (> ALL)? | Includes ORDER BY EID? | Correct? |
|---|---|---|---|
| (A) | No (uses >) | Yes | No |
| (B) | No (uses >) | No | No |
| (C) | Yes (uses > ALL) | Yes | Yes |
Therefore, only query (C) correctly answers the question.
| Concept | Description | Example Usage |
|---|---|---|
| Subquery | A query nested inside another SQL statement. It often returns a single value or a set of values used by the outer query. | WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE) |
| ALL Operator | Used with comparison operators (<, <=, >, >=, =, <>) and a subquery that returns a set of values. The condition is true if the comparison is true for every value in the set returned by the subquery. | WHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO = 20) |
| ANY Operator | Used with comparison operators and a subquery that returns a set of values. The condition is true if the comparison is true for at least one value in the set returned by the subquery. (Also written as SOME). | WHERE SALARY > ANY (SELECT SALARY FROM EMPLOYEE WHERE DEPTNO = 20) (Meaning: greater than the minimum salary in Dept 20) |
| ORDER BY Clause | Used to sort the result set of a query based on one or more columns in ascending (ASC) or descending (DESC) order. | ORDER BY EID ASC |
When using a subquery in a WHERE clause, especially one that returns multiple rows, you often need operators that can handle set-based comparisons:
In this specific question, the requirement "greater than the salary of all employees" explicitly demands the use of the > ALL operator.
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.