Consider the table employee(empId, name, department, salary) and the two queries Q1, Q2 below. Assuming that department 5 has more than one employee, and we want to find the employees who get higher salary than anyone in the department 5, which one of the statements is TRUE for any arbitrary employee table?
Q1: Select e.empId From employee e Where not exists (Select * From employee s Where s.department = "5" and s.salary >= e.salary)
Q2: Select e.empId From employee e Where e.salary > Any (Select distinct salary From employee s Where s.department = "5")
- Q1 is the correct query
- BQ2 is the correct query
- CBoth Q1 and Q2 produce the same answer
- DNeither Q1 nor Q2 is the correct query
Explanation
"Higher salary than anyone in department 5" means higher than EVERY department-5 employee (i.e. beats them all). Q1's NOT EXISTS clause finds employees for whom no department-5 employee has salary >= theirs — meaning they strictly exceed ALL department-5 salaries, matching the intended semantics. Q2's '> ANY' only requires exceeding at least ONE department-5 salary (the minimum), a weaker condition that doesn't match.
Source: GATE CS 2007 (2007)
This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.