COUNT() — How Many?
COUNT() is the simplest and most-used aggregate function in SQL. It tells you how many rows match a query.
Variants
COUNT(\*)
— counts every row, including ones with NULLs
COUNT(column) — counts rows where column is not NULL
COUNT(DISTINCT column) — counts the unique non-NULL values
Examples
Total number of customers: Customers who provided an email address: Number of distinct countries we have customers in:
Combined with WHERE
Active customers in the UK this year:
Why this matters for business
Counts answer a huge proportion of operational questions: How many orders did we ship yesterday? How many tickets are open? How many active users this week? They are the building block of dashboards.