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_nameTEXTrun_idINTEGERrows_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_name | run_id | rows_loaded | lowest_run_id |
|---|---|---|---|
| billing_rollup | 301 | 1500 | 304 |
| billing_rollup | 302 | 1500 | 304 |
| billing_rollup | 303 | 1300 | 304 |
| billing_rollup | 304 | 1100 | 304 |
| customer_sync | 201 | 850 | 204 |
| customer_sync | 202 | 850 | 204 |
| customer_sync | 203 | 850 | 204 |
| customer_sync | 204 | 650 | 204 |
| ingest_orders | 101 | 1000 | 104 |
| ingest_orders | 102 | 1200 | 104 |
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
Build the next SQL skill
CTEs & Window Functions
Practice modular CTE pipelines, deterministic window analytics, period comparisons, deduplication, frames, and gaps-and-islands.
SQL Aggregations
Build reliable SQL metrics from aggregate functions through grain, fan-out, weighted ratios, rollups, percentiles, and approximate counts.
ORDER BY & Sorting
Practice deterministic SQL ordering with tie-breakers, custom priorities, NULL placement, expressions, joined data, aggregates, and portable top-N patterns.
Open the interactive workspace and practice across SQL topics.