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
orders| id |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
Run result
| total |
|---|
| 4 |
Common issues
No. COUNT(*) counts rows, while COUNT(column) ignores rows where that column is NULL.
No. They return aggregate values only; use a join, ordering, or a window function when you need the complete row.
Put source-row predicates in WHERE and aggregate-result predicates in HAVING.
Tags
Was this explanation clear?
Thanks. This feedback will inform the next content review.