Practice · History · Hard

Reign length by era

For each era, return the era, the count of pharaohs as n, and the average reign length (reign_end − reign_start) as avg_len (1 decimal), longest average first.

Runs in your browser. No signup needed.

What you are given

pharaohs(name, reign_start, reign_end, era). Years are signed (negative = BCE), so the subtraction is a positive length. GROUP BY era.

Starting point

SELECT era, COUNT(*) AS n, ROUND(AVG(reign_end - reign_start), 1) AS avg_len
FROM pharaohs
GROUP BY era
ORDER BY avg_len DESC;

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

You can aggregate an expression, not just a column — AVG(reign_end − reign_start) averages a computed length. (SQLite docs)

More History challenges

Open it in the playground →