Aggregate Functions (COUNT, SUM, AVG, MIN, MAX) in SQL
Aggregate functions reduce a set of rows to a single computed value. COUNT, SUM, AVG, MIN, and MAX each collapse the row set differently, and each has specific behavior around NULLs, type coercion, and what counts as input.
Aggregate functions collapse a set of rows down to a single number.
You're in the database building a summary for the finance team. How many orders came in this month? What's total revenue? What's the average order value? These aren't questions you scroll through and count manually. The orders table might have hundreds of thousands of rows. You write one query and SQL does the counting and summing across the whole table at once.
The five functions that handle this are COUNT, SUM, AVG, MIN, and MAX. Each one scans a column across every row and returns a single result:
SELECT COUNT(*) AS order_count,
SUM(total_amount) AS total_revenue,
AVG(total_amount) AS avg_order_value
FROM ordersOne query, one row, three summary numbers. No matter how many rows the table has, a bare aggregate query always gives you one row back. MIN and MAX work the same way, returning the lowest or highest value in a column:
SELECT MIN(price) AS cheapest, MAX(price) AS most_expensive
FROM productsYou can narrow any aggregate to a specific subset with WHERE. SQL filters the rows first, then runs the aggregate on whatever survives:
SELECT AVG(total_amount) AS avg_delivered_value
FROM orders
WHERE status = 'delivered'Only delivered orders feed into the average. Every other row is gone before the aggregation runs. This is how you build conditional summaries: revenue for one region, count for one product category, average for one customer segment.
The one thing that trips people up: COUNT(*) and COUNT(column_name) look nearly identical but can return very different numbers.
COUNT(*) counts rows. Every row, regardless of what any column contains. COUNT(city) counts only rows where city is not NULL. On a customers table with 70 records where 9 are missing a city value, the two forms diverge:
The gap is your data quality signal. Any time you're auditing whether a column is fully populated, this pattern shows you immediately how many rows are missing a value.
The NULL-skipping logic extends across the whole family. SUM, AVG, MIN, and MAX all skip NULL values silently. If every row matching your filter has NULL in the target column, you get NULL back, not zero. That matters when the result feeds into a report that expects a number.
One more form worth knowing: COUNT(DISTINCT column) counts unique non-NULL values. If 200 orders came from 62 distinct customers, COUNT(DISTINCT customer_id) returns 62, not 200.
A table has 100 rows. 10 rows have NULL in the `city` column. What does COUNT(city) return?
10 Aggregate Functions (COUNT, SUM, AVG, MIN, MAX) practice problems
Write a query to return the total number of orders the platform has processed in a single column named `order_count`.
Write a query to return the summed list price as a single figure named `total_catalog_value`.
Write a query to return the lowest and highest product prices in a single row.
Write a query to return all three in a single row.
Write a query to return the average order value for delivered orders in a single column named `avg_delivered_value`.
Write a query to return the number of unique customers who appear in the orders history, in a single column named `unique_customers`.
Write a query to return the total unit count in a single column named `total_units`.
Write a query to return both counts in one row.
Write a query to return the exact average in a single column named `avg_quantity`.
Write a query to return the total list price of all products currently assigned to category `999`, in a single column named `total_price`.
These problems are part of the Aggregate Functions (COUNT, SUM, AVG, MIN, MAX) lesson in SQLMaxx, with instant grading and a worked solution on each.
Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.
That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.
Real problems. Not toy examples.
615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.
Write a query. Know if it's right in one second.
No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.
Stop guessing whether you actually know it.
SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.
Learn it once. Keep it for good.
Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.
Practice, feedback, mastery, review. That's the loop that turns reading into real skill.
Start freeNo account, no credit card. Start solving in under a minute.