← SQL and databases

Counting rows with COUNT

How many rows match?

Start this lesson

So far each row in the result came from one row in the table. Aggregate functions do something different: they squeeze many rows into one answer. COUNT(*) counts rows. COUNT(column) counts only rows where that column is not NULL. COUNT(DISTINCT column) counts different values.

SELECT COUNT(*) FROM books WHERE genre = 'Fiction';

WHERE first keeps the Fiction books, then COUNT(*) counts them. The answer is one row with the number 3.

COUNT(*) and COUNT(email) can differ: the first counts all members, the second only those with an email.