SQLPathLab
Practical SQL Guide · MySQL 8.0

COUNT(*), COUNT(column), and COUNT DISTINCT

The three forms answer row count, non-NULL value count, and unique-value count.

Problem and reasoning

COUNT(*) counts result rows, COUNT(column) ignores rows where that column is NULL, and COUNT(DISTINCT column) also removes duplicate non-NULL values. Choosing the wrong form changes the metric, not just its syntax.

On an orders table, COUNT(*) returns orders, while COUNT(DISTINCT customer_id) is closer to customers. After JOIN, first check whether the relationship multiplied rows.

Use business-specific aliases such as order_count, known_phone_count, or customer_count instead of naming every aggregate total.

Examples

Sample table

Table:customers
customers
nickname
NULL
Bobby
NULL

Before / Not recommended

SELECT COUNT(nickname) AS nickname_count FROM customers;

Before result

result
nickname_count
1

After / Recommended

SELECT COUNT(*) AS total_rows, COUNT(nickname) AS known_nicknames, COUNT(DISTINCT city) AS cities FROM customers;

After result

result
total_rowsknown_nicknamescities
3 1 3

Why it is better:Showing total rows, non-NULL nicknames, and distinct cities together prevents three business metrics from being confused as one COUNT.

Was this explanation clear?