201 — Aggregation and Grouping

Intermediate

Learn to summarize data with aggregate functions and GROUP BY. Transform thousands of rows into meaningful totals, averages, and counts — the foundation of every business report.

Learning Objectives

1
Use COUNT, SUM, AVG, MIN, and MAX to summarize data
2
Group rows with GROUP BY to create per-category summaries
3
Understand the difference between WHERE and HAVING
4
Combine grouping with sorting for polished reports
5
Count distinct values within groups
6
Build real-world reports using aggregation
7
Recognize the grain of a grouped result: one row per group
Step 1

Count rows with COUNT(*)

Start with the simplest aggregate: counting how many rows exist in a table.

Commands to Run

SELECT COUNT(*) AS total_customers FROM customers;
SELECT COUNT(*) AS total_orders FROM orders;
SELECT COUNT(*) AS total_products FROM products;

What This Does

COUNT(*) counts every row in the result set, including rows with NULL values. The AS keyword gives the output column a readable name (an alias). Aggregate functions collapse many rows into a single summary row.

Expected Outcome

You should see one row per query with the total count. For example, total_customers might show 50, total_orders might show 200, depending on your seed data.

Pro Tips

  • 1
    Always alias your aggregates with AS — 'COUNT(*)' as a column header is not helpful to anyone reading your report
  • 2
    COUNT(*) is the fastest way to count rows because the database doesn't need to inspect individual column values
  • 3
    Result grain here is simple: one aggregate query with no GROUP BY returns one summary row
Was this step helpful?

All Steps (0 / 12 completed)