GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 61

Salary greater than every department employee

Database Management Systems · SQL & Queries · Full GATE CSE 2007 paper

GATE 2007, Q61 2 marks · MCQ
Report issue

SQL & Queries

Question 61, GATE 2007: employee(empId,name,department,salary) has more than one employee in department 5. We want employees paid more than every department-5 employee. Compare:
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");
Which statement is intended for an arbitrary table under non-NULL salary semantics?
Options:
(A) Q1 is correct
(B) Q2 is correct
(C) Both always produce the same answer
(D) Neither is correct
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Approach. NOT EXISTS rejects the presence of a counterexample to “greater than everyone.” SQL ANY is existential: satisfying one comparison suffices. DISTINCT does not change that quantifier.

2. Derivation and interpretation. For each e, Q1 excludes e whenever any department-5 employee has salary >= e.salary. Thus it keeps exactly salaries strictly greater than the department maximum. Q2 uses > ANY, which means greater than at least one returned salary, not every salary. For department salaries 10 and 20, a salary 15 passes Q2 but not Q1. Hence A under the usual non-NULL salary interpretation. Strict SQL NULL semantics require qualification: NULL comparisons are UNKNOWN, so Q1 can admit NULL salaries or ignore unknown department salaries; no NOT NULL constraint is printed.

Original paper, page 15