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

Broadcast the Deterministic Top Run per Pipeline

Use FIRST_VALUE to broadcast the deterministic top run_id across each pipeline.

  • Window functions
  • Sorting

Exercise brief

Understand the request

Benchmarking engineer Each source row needs the chosen top run identifier for later row-level comparisons.

Each source row needs the chosen top run identifier for later row-level comparisons. Use FIRST_VALUE to broadcast the deterministic top run_id across each pipeline.

Return

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

Constraints

  • Order by rows_loaded DESC and run_id.
  • Declare the full-partition ROWS frame explicitly.

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

FIRST_VALUE returns a row value, not an aggregate maximum.

Hint 2

A stable key determines which peer supplies the identifier.

Hint 3

Use a full ROWS frame to make the intended partition scope explicit.

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, FIRST_VALUE(run_id) OVER (PARTITION BY pipeline_name ORDER BY rows_loaded DESC, run_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS top_run_id FROM pipeline_runs ORDER BY pipeline_name, run_id;

Why this works

FIRST_VALUE broadcasts an attribute from the first ordered row. The tie-breaker selects one peer deterministically and the explicit frame documents scope.

Success check

Every row in a pipeline carries the same deterministic top_run_id.

Expected result

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

pipeline_namerun_idrows_loadedtop_run_id
billing_rollup3011500301
billing_rollup3021500301
billing_rollup3031300301
billing_rollup3041100301
customer_sync201850201
customer_sync202850201
customer_sync203850201
customer_sync204650201
ingest_orders1011000102
ingest_orders1021200102

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.