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

Measure Relative Runtime Rank per Pipeline

Calculate duration_percent_rank per pipeline and round it to three decimals.

  • Window functions
  • Numeric functions
  • Type conversion
  • Sorting

Exercise brief

Understand the request

Performance analytics engineer A normalized runtime indicator must compare each run with others from the same pipeline while preserving duration peers.

A normalized runtime indicator must compare each run with others from the same pipeline while preserving duration peers. Calculate duration_percent_rank per pipeline and round it to three decimals.

Return

  • Return pipeline_name, run_id, duration_seconds, and duration_percent_rank.
  • Order by pipeline_name, duration_seconds, and run_id.

Constraints

  • Use PERCENT_RANK with duration_seconds only in the window order.
  • Do not break duration peers with run_id inside the window.

Data you will use

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

pipeline_runs

  • pipeline_nameTEXT
  • run_idINTEGER
  • duration_secondsINTEGER

Hints, when you need them

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

Hint 1

PERCENT_RANK is based on RANK, so peers should stay peers.

Hint 2

Partition by pipeline_name and order only by duration_seconds.

Hint 3

Round the function result, not the source duration.

Verified SQL answer

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

Reveal solution and explanation
SELECT pipeline_name, run_id, duration_seconds, ROUND(CAST(PERCENT_RANK() OVER (PARTITION BY pipeline_name ORDER BY duration_seconds) AS DECIMAL), 3) AS duration_percent_rank FROM pipeline_runs ORDER BY pipeline_name, duration_seconds, run_id;

Why this works

PERCENT_RANK computes (rank − 1)/(partition rows − 1). Because it is rank-based, tied durations share a result.

Success check

Equal durations share the same relative rank and each partition spans 0 to 1.

Expected result

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

pipeline_namerun_idduration_secondsduration_percent_rank
billing_rollup303900
billing_rollup304900
billing_rollup3021100.667
billing_rollup3011201
customer_sync204250
customer_sync201300.333
customer_sync202350.667
customer_sync203350.667
ingest_orders102450
ingest_orders103450

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.