GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATABASE MANAGEMENT SYSTEMS

Express universal SQL conditions by excluding counterexamples

3 questions solved the same way, newest first. Back to Database Management Systems

How these are solved

Use NOT EXISTS of a failing associated row or missing required row, with correlation intact. Empty associated sets require vacuous-truth checks; predicates differ across sources.

2 marks · NAT
Report issue

SQL & Queries

Question 56, GATE 2022: Student(sNo,sName,dNo): (S01,James,D01),(S02,Rocky,D01),(S03,Jackson,D02),(S04,Jane,D01),(S05,Milli,D02). Dept: (D01,CSE),(D02,EEE). Course(cNo,cName,dNo): (C11,DS,D01),(C12,OS,D01),(C21,DE,D02),(C22,PT,D02),(C23,CV,D03). Register pairs: S01:C11,C12; S02:C11; S03:C21,C22,C23; S04:C11,C12; S05:C11,C21. Count rows returned by SELECT * FROM Student AS S WHERE NOT EXIST (SELECT cNo FROM Course WHERE dNo = “D01” EXCEPT SELECT cNo FROM Register WHERE sNo = S.sNo). The printed spelling/quotes are retained to expose the source syntax issue.
Tap to enlarge
Answer & solution

Official answer: 2

The intended NOT EXISTS test succeeds when the set of D01 courses minus the student's registered courses is empty. The D01 course set is {C11, C12}. Therefore the intended query returns 2 student rows. Source syntax discrepancy: the printed SQL says NOT EXIST, missing the final S, and uses typographic double quotes around D01. Executable standard SQL needs NOT EXISTS and a single-quoted character literal. The official numeric answer assumes these obvious syntax repairs; the literal printed statement is not valid standard SQL.

The missing-D01-course sets are S01:∅, S02:{C12}, S03:{C11,C12}, S04:∅, S05:{C12}. NOT EXISTS accepts only empty sets. A student’s own department is not a filter in the outer query; only coverage of the D01 courses matters.

Original paper, page 38 · Answer key

2 marks · MCQ
Report issue

SQL & Queries

Question 54, GATE 2014: Schemas are employee(empId,empName,empDept) and customer(custId,custName,salesRepId,rating), with salesRepId referencing empId. Every employee sells to at least one customer. Query: SELECT empName FROM employee E WHERE NOT EXISTS (SELECT custId FROM customer C WHERE C.salesRepId=E.empId AND C.rating <> 'GOOD'). It returns names of employees with A at least one GOOD customer; B at most one GOOD customer; C no GOOD customers; D all customers GOOD.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Our answer: D assuming nonnull ratings

Step 1: For a fixed employee, the correlated subquery considers only that employee's customers, and finds rows whose rating is unequal to GOOD. NOT EXISTS keeps the employee exactly when this set is empty. Step 2: If ratings are non-NULL, having no customer rated other than GOOD means every customer is GOOD. The premise guarantees at least one customer, so the intended option is D. Step 3: The source does not declare rating NOT NULL. In SQL, NULL <> 'GOOD' is UNKNOWN, not TRUE, so a NULL-rated customer does not enter the subquery. An employee with only NULL-rated customers is still returned even though all-GOOD is not established. The precise query means “no own customer has a known non-GOOD rating”; retain the official D qualification and existing NULL discrepancy.

Original paper, page 66 · Answer key

2 marks · MCQ
Report issue

Salary greater than every department employee

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