Busiest Critic and Best Film

A film society publishes two facts in its monthly newsletter, and wants both from a single query:

  1. 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.
  2. 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.

ColumnTypeDescription
critic_id (key)INTEGERUnique id
critic_nameTEXTCritic name, unique

films — One row per film.

ColumnTypeDescription
film_id (key)INTEGERUnique id
film_titleTEXTFilm title, unique

film_ratings — One row per critic per film rated; a critic rates a film at most once.

ColumnTypeDescription
critic_id (key)INTEGERCritic who gave the rating
film_id (key)INTEGERFilm that was rated
starsINTEGERRating from 1 to 5
rated_onTEXTDate of the rating, 'YYYY-MM-DD'

Example

critics

critic_idcritic_name
1Okafor
2Lindqvist
3Baptiste

films

film_idfilm_title
1Iron Tide
2Night Ferry
3Blue Hour

film_ratings

critic_idfilm_idstarsrated_on
1142026-03-02
1252026-03-10
2132026-03-05
2242026-02-27
2352026-03-30
3342026-04-01
3222026-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

Solve the two questions separately first; each is a GROUP BY with ORDER BY ... LIMIT 1.
Put the alphabetical column second in ORDER BY to break ties: ORDER BY COUNT(*) DESC, critic_name.
In SQLite, a SELECT with its own ORDER BY/LIMIT must be wrapped as a subquery before it can be combined. Think about whether UNION or UNION ALL is right if the two answers are equal.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url