SQLPathLab
Aggregation · Basic

COUNT

counting rows or non-null values.

Overview

COUNT(*) counts rows after filtering, whereas COUNT(column) counts only rows where that column is not NULL. COUNT can summarize the full result or produce one count per GROUP BY group, and it returns zero rather than NULL. When counting business entities, verify that a JOIN has not multiplied rows even when the SQL is syntactically correct.

Basic syntax

SELECT COUNT(*) AS total FROM orders;

Examples

COUNT example

Count every row in orders and verify that the four sample records produce a total of 4.

Sample table

Table:orders
orders
id
1
2
3
4

Run result

result
total
4

Common issues

Are COUNT(*) and COUNT(column) the same?

No. COUNT(*) counts rows, while COUNT(column) ignores rows where that column is NULL.

Do MAX and MIN return the rest of the source row?

No. They return aggregate values only; use a join, ordering, or a window function when you need the complete row.

Where should filters go around aggregation?

Put source-row predicates in WHERE and aggregate-result predicates in HAVING.

Tags

AggregationCommon

Was this explanation clear?