Summarizing Data · COUNT · SUM · AVG · MIN · MAX
1. Grocery spending summary
Concept: COUNT · SUM · AVG · MIN · MAXshow
Aggregate functions squash many rows into one answer: COUNT(*) counts rows, SUM adds up a column, AVG takes the average, and MIN and MAX find the smallest and largest value. Without GROUP BY they look at every row that passed WHERE and return a single row. Averages often have a long tail of decimals, so round them: ROUND(AVG(col), 2).
Example (different data):
SELECT COUNT(*) AS rides, SUM(fare) AS total_fares, MAX(distance_km) AS longest FROM taxi_rides WHERE city = 'Lisbon';
Your task
The budgeting app shows a one-line summary card for each spending category, starting with groceries.
Using only transactions in the 'groceries' category, return one row with txn_count (how many there are), total_spent (sum of amount), avg_spent (average amount, rounded to 2 decimals), smallest (lowest amount) and largest (highest amount).
Starting Postgres in your browser…
Write a query and press Run Query (Ctrl/⌘ + Enter).