Counting rows with COUNT
So far every query has returned rows from a table. Aggregate functions do something different:
they summarise many rows into one answer. The simplest is COUNT(*), which counts rows:
SELECT COUNT(*) AS customers
FROM customersThe result is a single row with a single number. Add WHERE to count only some rows:
SELECT COUNT(*) AS cancelled_orders
FROM orders
WHERE status = 'Cancelled'COUNT(column) skips NULLs
COUNT(*) counts rows. COUNT(phone) counts the rows where phone isn't NULL. So the
difference between the two tells you how many values are missing:
SELECT COUNT(*) AS customers, COUNT(phone) AS with_phone
FROM customers