Aggregates & GROUP BY · Question 18 · Hard
Aggregates & GROUP BY question 18
Table Innings:
inn_id | batter | team | runs
1 | Kunal | Tigers | 45
2 | Kunal | Tigers | 0
3 | Lalit | Tigers | 72
4 | Mohan | Lions | 30
5 | Mohan | Lions | NULL
6 | Naveen | Lions | 58
7 | Naveen | Lions | 62
What does this query return?
SELECT team,
SUM(CASE WHEN runs >= 50 THEN 1 ELSE 0 END)
FROM Innings
GROUP BY team
ORDER BY team;
- Lions, 120; Tigers, 72
- Lions, 4; Tigers, 3
- Lions, 2; Tigers, 1
- Lions, 3; Tigers, 1
Answer and explanation
Answer: C. Lions, 2; Tigers, 1
Explanation: The CASE gives 1 for an innings of 50 or more and 0 otherwise, so the SUM counts fifties. Lions have 58 and 62; Tigers have 72. For the NULL innings the test is not true, so it falls to ELSE 0.
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
- Table Attendance: student | subject | month_name | days Pooja | Maths | Jan |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Sales: sale_id | rep | region | amount 1 | Asif | North | 400 2 | Bina |…
- Table Sales(sale_id, rep, region, amount) has the regions North, South and…
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