COUNT()

COUNT() is an aggregate function that returns how many rows match a query. COUNT(*) counts all rows, while COUNT(column) counts only rows where that column is not NULL.

SQL

It is one of the most common ways to answer 'how many?' questions, such as how many orders were placed or how many users signed up.

Combined with GROUP BY, COUNT() gives you a count per group, like the number of orders per customer.

Syntax

SELECT COUNT(*) FROM table_name WHERE condition;

Examples

SQL

SELECT COUNT(*) AS total
FROM users
WHERE age >= 18;

Output:

total
-----
42

Learn it in a lesson

See also

Back to the Dictionary