Lesson 4 of 10 · 8 min · 2 hands-on steps

Counting & Summarizing (Aggregates)

COUNT, SUM, AVG, MIN and MAX turn many rows into one answer.

One answer from many rows

SQL · try it
SELECT COUNT(*) FROM employees;
SQL · try it
SELECT
  COUNT(*)            AS people,
  SUM(salary)         AS payroll,
  ROUND(AVG(salary))  AS average,
  MIN(hired)          AS first_hire
FROM employees;

AS gives a result column a readable name. Get in the habit; reports and apps read columns by name.

Combine with WHERE

WHERE filters first, then the aggregate runs on what's left:

SQL · try it
SELECT COUNT(*) AS lagos_customers
FROM customers WHERE city = 'Lagos';

COUNT(*) vs COUNT(column)

COUNT(*) counts rows. COUNT(column) counts rows where that column is not NULL. COUNT(DISTINCT column) counts unique values:

SQL · try it
SELECT COUNT(DISTINCT city) AS cities FROM customers;

🧪 Practice: 2 steps

0 / 2 done

Step 1 of 2 · How many orders?

Tables: orders(id, customer_id, product_id, quantity, ordered_on).

Return the total number of orders as a column named order_count.

SQL
SELECT * FROM orders;

Output appears here. Press Run or Ctrl/⌘ + Enter.

✅ Check your understanding

  1. Which function adds up the values in a column?

  2. What does AS do in SELECT COUNT(*) AS total?

Create a free account to track your progress.