Aggregation and Grouping · Problem 33 of 118

Transactions by Kind

MediumAggregation and GroupingBank data

For each transaction kind, return the kind, transactions (how many) and total_cents (the sum of amount_cents). Sort by total_cents from high to low.

Hint 1

Group by kind and use COUNT(*) and SUM(...).

Reference solution

SELECT kind,
       COUNT(*) AS transactions,
       SUM(amount_cents) AS total_cents
FROM transactions
GROUP BY kind
ORDER BY total_cents DESC;

Several aggregates can share one GROUP BY. Note that amount_cents is always positive in this table; the kind tells you whether money came in (deposit, interest, transfer_in) or went out (withdrawal, fee, transfer_out). Summing the column without looking at kind would add incoming and outgoing money together, which is rarely meaningful.