Consider the table employee(empId, name, department, salary) and the two…
2007
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”)
- A.
Q1 is the correct query
- B.
Q2 is the correct query
- C.
Both Q1 and Q2 produce the same answer.
- D.
Neither Q1 nor Q2 is the correct query
Attempted by 47 students.
Show answer & explanation
Correct answer: A
Answer: The query that uses NOT EXISTS (the first query) is the correct query.
Why the first query is correct:
The NOT EXISTS subquery checks that there is no employee s in department 5 with s.salary >= e.salary.
That condition is equivalent to saying for all employees s in department 5, s.salary < e.salary; in other words, e.salary is greater than every salary in department 5.
This matches the requirement to find employees who get a higher salary than anyone in department 5.
Why the second query is incorrect:
The > ANY condition returns true when the employee's salary is greater than at least one salary in department 5.
That allows employees whose salary is higher than some but not all employees in department 5, so it does not satisfy the requirement.
Example: If department 5 has salaries 100 and 200, an employee with salary 150 satisfies the > ANY condition (150 > 100) but is not higher than everyone in department 5 (150 < 200).
Conclusion: use the NOT EXISTS form (the first query) to find employees whose salary is greater than every salary in department 5.
A video solution is available for this question — log in and enroll to watch it.