Ranking & NTH Value SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Read the Partition Tail with LAST_VALUE

Use LAST_VALUE with a full-partition frame to return lowest_run_id.

  • Window functions
  • Sorting

Exercise brief

Understand the request

SQL interview panel A diagnostic needs the lowest-throughput run identifier repeated on every row of its pipeline.

A diagnostic needs the lowest-throughput run identifier repeated on every row of its pipeline. Use LAST_VALUE with a full-partition frame to return lowest_run_id.

Return

  • Return pipeline_name, run_id, rows_loaded, and lowest_run_id.
  • Order by pipeline_name and run_id.

Constraints

  • Order by rows_loaded DESC and run_id.
  • End the frame at UNBOUNDED FOLLOWING, not CURRENT ROW.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

pipeline_runs

  • pipeline_nameTEXT
  • run_idINTEGER
  • rows_loadedINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

LAST_VALUE reads the last row of the current frame, not automatically the partition.

Hint 2

The default ordered frame often ends at the current peer group.

Hint 3

Specify ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT pipeline_name, run_id, rows_loaded, LAST_VALUE(run_id) OVER (PARTITION BY pipeline_name ORDER BY rows_loaded DESC, run_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lowest_run_id FROM pipeline_runs ORDER BY pipeline_name, run_id;

Why this works

This exercise targets a common window-frame trap: with the default frame, LAST_VALUE can return the current row or peer tail instead of the partition tail.

Success check

Every row in each pipeline receives the same true partition-tail run_id.

Expected result

Use this output to verify values, aliases, ordering, and row count.

pipeline_namerun_idrows_loadedlowest_run_id
billing_rollup3011500304
billing_rollup3021500304
billing_rollup3031300304
billing_rollup3041100304
customer_sync201850204
customer_sync202850204
customer_sync203850204
customer_sync204650204
ingest_orders1011000104
ingest_orders1021200104

Previewing 10 of 12 expected rows. Run the query in the editor to inspect the full result.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.