runtime
12 / 88

lesson 12 Calculations & data quality · beginner

MIN, MAX & the grain of a result

Find extremes and define what each output row represents.

the idea

MIN and MAX summarize the smallest and largest values. The grain describes what one output row means. Adding GROUP BY changes that grain from one overall row to one row per group.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

watch_events5 columns · 26 rows
idinteger · keyviewer_idintegervideo_idintegerwatched_attextwatch_secondsinteger
1112025-01-01 10:00:00300
2122025-01-01 11:00:00450
3212025-01-02 12:00:00600
4332025-01-02 13:00:00120
5142025-01-02 14:00:00700

showing 5 of 26 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT
  MIN(watch_seconds) AS shortest_watch,
  MAX(watch_seconds) AS longest_watch
FROM watch_events;

Watch for: A maximum value alone does not identify every other column from that row.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Scan the watch lengths. Each event has watch_seconds. One overall MIN/MAX summarizes the whole table; GROUP BY later changes the grain.

    step-1.sql
    SELECT id, video_id, watch_seconds
    FROM watch_events
    ORDER BY id
    LIMIT 10;
  2. Summarize at the overall grain. Without GROUP BY, MIN and MAX return one row for the entire result. That grain is “the whole table.”

    step-2.sql
    SELECT
      MIN(watch_seconds) AS shortest_watch,
      MAX(watch_seconds) AS longest_watch
    FROM watch_events;

challenge 1

Return shortest_watch and longest_watch: minimum and maximum watch_seconds across all watch events.

Return shortest_watchlongest_watchany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.