Aggregation & summary — quiz
8 questions covering this module. Aggregate functions, GROUP BY, HAVING and conditional aggregation.
COUNT(*) is 500 and COUNT(email) is 480. Why?
- Duplicate rows
- 20 rows have a null email
- A join issue
- An index issue
Answer: 20 rows have a null email — COUNT on a column ignores nulls.
Why does WHERE SUM(total) > 1000 fail?
- SUM needs GROUP BY
- WHERE is evaluated before aggregation
- SUM is not a function
- It needs parentheses
Answer: WHERE is evaluated before aggregation — The aggregate does not exist at the point WHERE is applied.
Which query is valid?
- SELECT region, city, SUM(t) FROM o GROUP BY region
- SELECT region, SUM(t) FROM o GROUP BY region
- SELECT region, city FROM o GROUP BY SUM(t)
- SELECT SUM(region) FROM o GROUP BY t
Answer: SELECT region, SUM(t) FROM o GROUP BY region — Every non-aggregated column must appear in GROUP BY.
Adding a column to GROUP BY generally:
- Reduces the row count
- Increases the row count
- Has no effect
- Causes an error
Answer: Increases the row count — Finer grouping produces more, smaller groups.
You need only regions with revenue above 100k. Use:
- WHERE
- HAVING
- ORDER BY
- DISTINCT
Answer: HAVING — The condition is on an aggregate, which exists only after grouping.
Putting a row-level filter in HAVING instead of WHERE:
- Errors
- Usually works but is slower
- Is always faster
- Changes the result
Answer: Usually works but is slower — More rows reach the grouping step and are then thrown away.
Why is ELSE omitted in COUNT(CASE WHEN x THEN 1 END)?
- It is optional syntax
- The resulting null makes COUNT skip non-matching rows
- ELSE is invalid there
- To improve performance
Answer: The resulting null makes COUNT skip non-matching rows — COUNT ignores nulls, so the missing ELSE is what makes the count selective.
Conditional aggregation replaces:
- Indexes
- Multiple separate queries or a self-join
- GROUP BY
- Window functions
Answer: Multiple separate queries or a self-join — It computes several conditional measures in one pass.