Pitfall
COUNT(*) vs COUNT(column)
COUNT(column) skips NULLs; COUNT(*) counts rows.To count rows, use COUNT(*). Use COUNT(column) only when you mean "rows where this column is filled",
and COUNT(DISTINCT column) for distinct values.
SELECT COUNT(*) AS all_rows,
COUNT(city) AS with_city,
COUNT(DISTINCT city) AS cities
FROM customers;
| all_rows | with_city | cities |
|---|---|---|
| 400 | 382 | 27 |
Practise:COUNT(*) or COUNT(city)?