Practice · History · Medium

Rank the longest reigns

Return the top 5 pharaohs by reign length: name, the length as len, and a RANK() as rnk (1 = longest). Order by rnk.

Runs in your browser. No signup needed.

What you are given

pharaohs(name, reign_start, reign_end). RANK() OVER (ORDER BY (reign_end − reign_start) DESC). A window function ranks without collapsing rows.

Starting point

SELECT name, (reign_end - reign_start) AS len,
       RANK() OVER (ORDER BY (reign_end - reign_start) DESC) AS rnk
FROM pharaohs
ORDER BY rnk
LIMIT 5;

The reference solution is checked automatically when you run your query on the practice page — results are compared row by row, and order matters for this one.

Did you know

Pepi II is traditionally credited with ~94 years on the throne — possibly the longest reign in history, though some scholars argue for ~64. (Turin King List)

More History challenges

Open it in the playground →