Busiest Critic and Best Film
A film society publishes two facts in its monthly newsletter, and wants both from a single query:
- The name of the critic who has rated the most films overall (all dates). If several critics tie, pick the name that comes first alphabetically.
- The title of the film with the highest average star rating among ratings made in March 2026. If several films tie, pick the title that comes first alphabetically.
Return a single column named result with exactly these two rows, in any order. The two values can be the same text (a critic may share a film's title), and both rows must still appear. Every dataset has at least one rating in March 2026.
Tables
critics — One row per critic in the society.
| Column | Type | Description |
|---|---|---|
critic_id (key) | INTEGER | Unique id |
critic_name | TEXT | Critic name, unique |
films — One row per film.
| Column | Type | Description |
|---|---|---|
film_id (key) | INTEGER | Unique id |
film_title | TEXT | Film title, unique |
film_ratings — One row per critic per film rated; a critic rates a film at most once.
| Column | Type | Description |
|---|---|---|
critic_id (key) | INTEGER | Critic who gave the rating |
film_id (key) | INTEGER | Film that was rated |
stars | INTEGER | Rating from 1 to 5 |
rated_on | TEXT | Date of the rating, 'YYYY-MM-DD' |
Example
critics
| critic_id | critic_name |
|---|---|
| 1 | Okafor |
| 2 | Lindqvist |
| 3 | Baptiste |
films
| film_id | film_title |
|---|---|
| 1 | Iron Tide |
| 2 | Night Ferry |
| 3 | Blue Hour |
film_ratings
| critic_id | film_id | stars | rated_on |
|---|---|---|---|
| 1 | 1 | 4 | 2026-03-02 |
| 1 | 2 | 5 | 2026-03-10 |
| 2 | 1 | 3 | 2026-03-05 |
| 2 | 2 | 4 | 2026-02-27 |
| 2 | 3 | 5 | 2026-03-30 |
| 3 | 3 | 4 | 2026-04-01 |
| 3 | 2 | 2 | 2026-03-15 |
Expected output
| result |
|---|
| Lindqvist |
| Blue Hour |
Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Subqueries & CTEs · problem 28 of 70 · SQLite
The idea
These are two independent top-1 questions. Solve each one, then stack the answers.
Busiest critic: join critics to their ratings, group by critic, and sort by COUNT(*) DESC, critic_name. The second sort key is the tie-break, so LIMIT 1 returns the alphabetically first name among the leaders.
Best film in March: filter ratings to the month with a half-open range, rated_on >= '2026-03-01' AND rated_on < '2026-04-01', which works for ISO dates. Then group by film and sort by AVG(stars) DESC, film_title.
To return both, combine them with UNION ALL. In SQLite, an ORDER BY or LIMIT inside one branch of a compound query is not allowed, so each branch is wrapped as SELECT result FROM (...). Use UNION ALL, not UNION: UNION removes duplicates and would merge the rows when a critic and a film share a name.
The alternative computes counts and averages in CTEs, keeps the rows equal to the maximum, and takes MIN(name) as the tie-break.
Solution
SELECT result FROM (
SELECT c.critic_name AS result
FROM critics AS c
JOIN film_ratings AS r ON r.critic_id = c.critic_id
GROUP BY c.critic_id, c.critic_name
ORDER BY COUNT(*) DESC, c.critic_name
LIMIT 1
)
UNION ALL
SELECT result FROM (
SELECT f.film_title AS result
FROM films AS f
JOIN film_ratings AS r ON r.film_id = f.film_id
WHERE r.rated_on >= '2026-03-01'
AND r.rated_on < '2026-04-01'
GROUP BY f.film_id, f.film_title
ORDER BY AVG(r.stars) DESC, f.film_title
LIMIT 1
);
Another way
WITH critic_counts AS (
SELECT c.critic_name, COUNT(*) AS n
FROM critics AS c
JOIN film_ratings AS r ON r.critic_id = c.critic_id
GROUP BY c.critic_id, c.critic_name
),
march_avgs AS (
SELECT f.film_title, AVG(r.stars) AS avg_stars
FROM films AS f
JOIN film_ratings AS r ON r.film_id = f.film_id
WHERE strftime('%Y-%m', r.rated_on) = '2026-03'
GROUP BY f.film_id, f.film_title
)
SELECT MIN(critic_name) AS result
FROM critic_counts
WHERE n = (SELECT MAX(n) FROM critic_counts)
UNION ALL
SELECT MIN(film_title)
FROM march_avgs
WHERE avg_stars = (SELECT MAX(avg_stars) FROM march_avgs);
Why common answers fail
UNION removes duplicates, so when the top critic and the top film have the same name only one row comes back.
SELECT result FROM (
SELECT c.critic_name AS result
FROM critics AS c JOIN film_ratings AS r ON r.critic_id = c.critic_id
GROUP BY c.critic_id, c.critic_name
ORDER BY COUNT(*) DESC, c.critic_name LIMIT 1
)
UNION
SELECT result FROM (
SELECT f.film_title AS result
FROM films AS f JOIN film_ratings AS r ON r.film_id = f.film_id
WHERE r.rated_on >= '2026-03-01' AND r.rated_on < '2026-04-01'
GROUP BY f.film_id, f.film_title
ORDER BY AVG(r.stars) DESC, f.film_title LIMIT 1
);
Has no alphabetical tie-break, so a tie returns whichever row SQLite happens to produce first.
SELECT result FROM (
SELECT c.critic_name AS result
FROM critics AS c JOIN film_ratings AS r ON r.critic_id = c.critic_id
GROUP BY c.critic_id, c.critic_name
ORDER BY COUNT(*) DESC LIMIT 1
)
UNION ALL
SELECT result FROM (
SELECT f.film_title AS result
FROM films AS f JOIN film_ratings AS r ON r.film_id = f.film_id
WHERE r.rated_on >= '2026-03-01' AND r.rated_on < '2026-04-01'
GROUP BY f.film_id, f.film_title
ORDER BY AVG(r.stars) DESC LIMIT 1
);
Forgets the March 2026 filter, so the film average uses ratings from every month.
SELECT result FROM (
SELECT c.critic_name AS result
FROM critics AS c JOIN film_ratings AS r ON r.critic_id = c.critic_id
GROUP BY c.critic_id, c.critic_name
ORDER BY COUNT(*) DESC, c.critic_name LIMIT 1
)
UNION ALL
SELECT result FROM (
SELECT f.film_title AS result
FROM films AS f JOIN film_ratings AS r ON r.film_id = f.film_id
GROUP BY f.film_id, f.film_title
ORDER BY AVG(r.stars) DESC, f.film_title LIMIT 1
);
What the interviewer is checking
Tests clean top-N with deterministic tie-breaking, date-range filtering, and knowing that UNION deduplicates while UNION ALL does not.
Mistakes to avoid
- UNION instead of UNION ALL, losing a row when the two answers match.
- ORDER BY ... LIMIT 1 without a tie-breaker, which is non-deterministic.
- Writing ORDER BY/LIMIT directly inside a UNION branch, which SQLite rejects; wrap each branch in a subquery.
Follow-ups to expect
- Add a label column so readers know which row is which (SELECT 'critic' AS kind, ...).
- Only consider films with at least 3 March ratings (HAVING COUNT(*) >= 3).
- Return all tied leaders instead of one (keep rows equal to the MAX, or DENSE_RANK() = 1).
In real work
Newsletter and dashboard headline tiles ("top seller this month", "most active user") are often several independent top-1 queries stacked into one result.