Subqueries & CTEs · Question 14 · Medium

Subqueries & CTEs question 14

Table Marks:
student | subject | score
Kavya | Maths | 88
Kavya | Physics | 70
Lokesh | Maths | 92
Lokesh | Physics | 65
Mitali | Maths | 75
Mitali | Physics | 95

What does this query return?

SELECT subject, student
FROM Marks m
WHERE score = (SELECT MAX(score)
               FROM Marks x
               WHERE x.subject = m.subject)
ORDER BY subject;
  1. Physics, Mitali
  2. Maths, Lokesh; Physics, Mitali
  3. Maths, Kavya; Physics, Mitali
  4. Maths, Lokesh; Physics, Kavya
Answer and explanation

Answer: B. Maths, Lokesh; Physics, Mitali

Explanation: For each row the subquery finds the highest score in that row's subject: 92 in Maths and 95 in Physics. The rows that equal their subject's maximum are Lokesh in Maths and Mitali in Physics.

Previous: Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans… · Next: In the query below, how is the inner query logically evaluated?

Practise subqueries & ctes

Try a fresh set of subqueries & ctes questions with an explanation after every answer, free and without signing up: Subqueries & CTEs practice. Or sit a full placement mock test.

More subqueries & ctes questions

More sql practice

Window Functions · NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge