Beginner · Databases and SQL basics · topic 20 of 54 · 25 min
Counting and totals: aggregate functions
Explanation
Many rows in, one answer out
So far each query returned rows from the table. An aggregate function looks at many rows and returns a single value. The shop owner does not want 10,000 bills; she wants "how many orders today and how much money".
| Function | Gives | Example |
|---|---|---|
| COUNT(*) | number of rows | SELECT COUNT(*) FROM orders |
| COUNT(column) | number of rows where that column is NOT NULL | SELECT COUNT(rating) FROM orders |
| SUM(column) | total | SELECT SUM(amount) FROM orders |
| AVG(column) | average | SELECT AVG(amount) FROM orders |
| MIN / MAX | smallest / largest | SELECT MAX(amount) FROM orders |
SELECT COUNT(*) AS orders, SUM(amount) AS total
FROM orders
WHERE city = 'Pune';COUNT(*) vs COUNT(column)
COUNT(*) counts every row. COUNT(rating) counts only the rows where rating has a value; rows where it is NULL are skipped. SUM, AVG, MIN and MAX also skip NULLs. So if 8 orders exist and only 5 were rated, COUNT(*) is 8 and COUNT(rating) is 5.
Rounding averages
An average often has many decimals, like 825.625. ROUND(value, 2) keeps 2 digits after the point: 825.63. For money, round to 2 places; for marks, 1 place is often enough.
SELECT ROUND(AVG(amount), 2) AS average_bill FROM orders;A first look at GROUP BY
GROUP BY city splits the rows into one group per city and runs the aggregate on each group. You get one result row per city. Every column in SELECT must be either in GROUP BY or inside an aggregate function. You will go deeper into GROUP BY in the next module.
SELECT city, COUNT(*) AS orders, SUM(amount) AS total
FROM orders
GROUP BY city
ORDER BY city;Watch the lesson
Counting and totals: aggregate functions · 1/6
Today we turn many rows into one answer: counts, totals and averages.
Prefer a video by a teacher? Find free videos on “sql count sum avg min max group by tutorial for beginners” on YouTube ↗
In real life: A kirana shop's daily report
At 10 pm, a kirana shop's billing app shows the owner one screen: 143 bills today, total 48,250 rupees, average bill 337.41 rupees, biggest bill 3,200 rupees, and a small table of sales per payment type (cash, UPI, card). Each number is one aggregate query, and the table is a GROUP BY payment_type. The database does the adding, so the app stays fast even after years of bills.
SELECT COUNT(*) AS bills, SUM(amount) AS total,
ROUND(AVG(amount), 2) AS average, MAX(amount) AS biggest
FROM bills
WHERE bill_date = '2026-10-19';Practice (0/3 done)
The orders table has id, customer, city, amount and rating (rating is NULL when the customer did not rate). In ONE query, return two numbers: how many orders there are in total, and how many orders have a rating.
Return one row with four columns, in this order: the total of all amounts, the average amount rounded to 2 decimals, the smallest amount and the largest amount.
Show one row per city with three columns: city, the number of orders, and the total amount. Sort by city from A to Z.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.