Consider the table employee(empId, name, department, salary) and the two queries below. Assuming that department has more than one employee, and we want to find the employees who get higher salary than anyone in the department which one of the statements is TRUE for any arbitrary employee table?
Select e.empId
From employee e
Where not exists
(Select * From employee s Where s.department = "5" and s.salary >= e.salary)
|
|
Select e.empId
From employee e
Where e.salary > Any
(Select distinct salary From employee s Where s.department = "5")
|
View worked solution
Answer: A
To find the employees who earn a higher salary than anyone in department 5, we need to find employees whose salary is strictly greater than the maximum salary of any employee in department 5.
Analyzing Query :
Select e.empId
From employee e
Where not exists
(Select * From employee s Where s.department = "5" and s.salary >= e.salary)
- The subquery searches for any employee in department 5 whose salary is greater than or equal to employee 's salary ().
- The
NOT EXISTSclause ensures that no such employee exists in department 5. - Mathematically, this means for every employee in department 5, .
- Thus, correctly identifies employees who have a higher salary than everyone in department 5.
Analyzing Query :
Select e.empId
From employee e
Where e.salary > Any
(Select distinct salary From employee s Where s.department = "5")
- The
> ANYoperator returns true if is greater than at least one salary in department 5. - This is equivalent to finding employees whose salary is greater than the minimum salary in department 5, which does not satisfy the requirement of being higher than anyone in department 5.
Therefore, only is the correct query.
Free practice · Your answers on this page are not saved.
