Consider the following relation schema R along with the tuples. Employee(name,…
2017
Consider the following relation schema R along with the tuples.
Employee(name, salary) = {<e1, 10000>, <e2, 5000>, <e3, 2500>, <e4, 7500>, <e5, 8900>, <e6, 9800>}
What is the output of following SQL query?
SELECT name, MAX(salary) FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee);
Answer: C. <e6, 9800> — Concept: A scalar subquery inside a WHERE clause is fully evaluated first, producing a single value that the outer query then filters against. Once the WHERE…
- A.
<e3, 2500>
- B.
<e5, 8900>
- C.
<e6, 9800>
- D.
<e1, 10000>
Attempted by 516 students.
Show answer & explanation
Correct answer: C
Concept: A scalar subquery inside a WHERE clause is fully evaluated first, producing a single value that the outer query then filters against. Once the WHERE clause has filtered the rows, any aggregate function (MAX, MIN, COUNT, etc.) in the outer SELECT operates only on that filtered subset of rows, not on the whole table.
Application: working through the query step by step —
Evaluate the inner subquery first:
SELECT MAX(salary) FROM Employeescans all six tuples and returns 10000 (the salary of e1).Apply the outer
WHEREfilter: keep only rows withsalary < 10000. This keeps e2 (5000), e3 (2500), e4 (7500), e5 (8900), and e6 (9800); it drops e1 (10000) because 10000 is not strictly less than 10000.Apply the outer aggregate
MAX(salary)over exactly this filtered set of five tuples — the largest value among {5000, 2500, 7500, 8900, 9800} is 9800, contributed by e6.So the query returns the single row <e6, 9800>.
Cross-check: sorting the filtered salaries — 2500, 5000, 7500, 8900, 9800 — confirms 9800 is the unique largest value in that set, so no other filtered tuple could produce a larger MAX(salary). (Note: strict ANSI SQL, PostgreSQL, Oracle, and MySQL with ONLY_FULL_GROUP_BY reject this query outright, since name is not functionally dependent on the aggregate without a GROUP BY. A dialect such as SQLite, which specifically permits a bare column alongside MIN/MAX and deterministically returns the row that produced the extreme value, DOES guarantee this exact pairing — non-strict MySQL, by contrast, only guarantees the aggregate value and leaves the paired name officially unspecified. This question follows that SQLite-style / exam-convention reading, under which the row of the extreme salary is returned together with it — exactly the concept these PYQ-style subquery questions are testing.)
Explore the full course: Iocl Engineers Officers Grade A Paper 2