Subqueries & CTEs · Question 17 · Hard

Subqueries & CTEs question 17

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?

WITH tot AS (
  SELECT student, SUM(score) AS total
  FROM Marks
  GROUP BY student
)
SELECT student
FROM tot
WHERE total = (SELECT MAX(total) FROM tot);
  1. Mitali
  2. Kavya
  3. Lokesh
  4. Lokesh; Mitali
Answer and explanation

Answer: A. Mitali

Explanation: The CTE gives totals Kavya 158, Lokesh 157 and Mitali 170. It is used twice: once to find the highest total, 170, and once to find who has it. Lokesh has the best single Maths score but not the best total.

Previous: Table Marks: student | subject | score Kavya | Maths | 88 Kavya | Physics | 70… · Next: Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…

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