Aggregation and Grouping · Problem 29 of 118

Balances of Open Accounts

EasyAggregation and GroupingBank data

For accounts with status = 'open', return one row with: accounts (how many), total_cents (sum of balances), average_cents (average balance, rounded to a whole number), smallest_cents and largest_cents.

Hint 1

ROUND(AVG(x)) rounds to a whole number.

Reference solution

SELECT COUNT(*) AS accounts,
       SUM(balance_cents) AS total_cents,
       ROUND(AVG(balance_cents)) AS average_cents,
       MIN(balance_cents) AS smallest_cents,
       MAX(balance_cents) AS largest_cents
FROM accounts
WHERE status = 'open';

WHERE filters the rows before they are aggregated, so the five numbers describe open accounts only. An aggregate without GROUP BY collapses the whole (filtered) table into a single row.