Aggregates & GROUP BY · Question 15 · Medium

Aggregates & GROUP BY question 15

Table Attendance:
student | subject | month_name | days
Pooja | Maths | Jan | 20
Pooja | Maths | Feb | 18
Pooja | Physics | Jan | 15
Rahul | Maths | Jan | 22
Rahul | Physics | Jan | 19
Rahul | Physics | Feb | 21

What does this query return?

SELECT subject, month_name, SUM(days)
FROM Attendance
GROUP BY subject, month_name
HAVING COUNT(*) = 2
ORDER BY subject, month_name;
  1. Maths, Jan, 42; Physics, Jan, 34
  2. Maths, Feb, 18; Maths, Jan, 42; Physics, Feb, 21; Physics, Jan, 34
  3. Maths, 60; Physics, 55
  4. Maths, Jan, 2; Physics, Jan, 2
Answer and explanation

Answer: A. Maths, Jan, 42; Physics, Jan, 34

Explanation: Groups are (subject, month) pairs. Only Maths-Jan (20 + 22 = 42) and Physics-Jan (15 + 19 = 34) contain two rows; the Feb groups have one row each and are removed by HAVING.

Previous: Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |… · Next: Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…

Practise aggregates & group by

Try a fresh set of aggregates & group by questions with an explanation after every answer, free and without signing up: Aggregates & GROUP BY practice. Or sit a full placement mock test.

More aggregates & group by questions

More sql practice

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

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