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
SELECT COUNT(*) FROM employees;
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:
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:
SELECT COUNT(DISTINCT city) AS cities FROM customers;
🧪 Practice: 2 steps
0 / 2 doneStep 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.
SELECT * FROM orders;
Output appears here. Press Run or Ctrl/⌘ + Enter.
Step 2 of 2 · Salary stats
Return three columns about Engineering (department_id = 1): lowest, highest and average salary (average rounded to a whole number).
SELECT salary FROM employees WHERE department_id = 1;
Output appears here. Press Run or Ctrl/⌘ + Enter.
✅ Check your understanding
Which function adds up the values in a column?
What does AS do in SELECT COUNT(*) AS total?
Create a free account to track your progress.