BeginnerCoder

Lesson 19 — Ranking with a subquery

Ranking without window functions

A rank can be computed with a correlated subquery: rank = 1 + how many rows have a higher score. Ties get the same rank automatically, and the next rank skips (1, 2, 2, 4) — this is called "competition ranking".

SELECT runner,
  1 + (SELECT COUNT(*) FROM races r2 WHERE r2.secs < r.secs) AS place
FROM races r

Why it matters: it shows how ranking really works, and it runs on old databases without window functions.

Want it another way?
Task

Fill in the subquery condition: count rows with more points than the current row. Expected: Bo and Cy share rank 2.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with SQL Deep Dive

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.