Practice · History · Hard
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.
pharaohs(name, reign_start, reign_end, era). Years are signed (negative = BCE), so the subtraction is a positive length. GROUP BY era.
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.
You can aggregate an expression, not just a column — AVG(reign_end − reign_start) averages a computed length. (SQLite docs)