# COUNT(*) vs COUNT(column) vs COUNT(DISTINCT)

Source: https://sqlstreak.com/sql/count-star-vs-count-column · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**Short answer:** `COUNT(*)` counts rows. `COUNT(column)` counts the rows where that column is not NULL. `COUNT(DISTINCT column)` counts the different non-NULL values. `COUNT(1)` is the same as `COUNT(*)`.

## Side by side

| | COUNT(*) | COUNT(column) | COUNT(DISTINCT column) |
| --- | --- | --- | --- |
| Counts | Rows | Non-NULL values | Different non-NULL values |
| NULLs | Counted (it's a row) | Skipped | Skipped |
| After a LEFT JOIN, for a row with no match | 1 | 0 (count a right-hand column) | 0 |
| Typical use | How many rows | How many have a value filled in | How many different ones |

## Examples

### Three answers from one table

20 pets, of which some have no recorded breed (NULL), and fewer different breeds than pets.

```sql
SELECT COUNT(*) AS pets,
       COUNT(breed) AS with_breed,
       COUNT(DISTINCT breed) AS breeds,
       COUNT(DISTINCT species) AS species
FROM pets;
```

Result (both engines):

| pets | with_breed | breeds | species |
| --- | --- | --- | --- |
| 20 | 14 | 13 | 4 |

### The LEFT JOIN trap

Counting orders per customer. Customers with no orders still produce one row with NULLs, so `COUNT(*)` says 1. Count a column from the right-hand table instead.

```sql
SELECT c.name,
       COUNT(*) AS count_star,
       COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY orders, c.name;
```

Result (both engines):

| name | count_star | orders |
| --- | --- | --- |
| Diego Santos | 1 | 0 |
| Nora Lindqvist | 1 | 0 |
| Lena Fischer | 1 | 1 |
| Priya Nair | 1 | 1 |
| Ellie Park | 2 | 2 |
| Marcus Bell | 2 | 2 |
| Sam Okafor | 2 | 2 |
| Tomás Herrera | 2 | 2 |

(8 of 10 rows)

## When to use which

- **COUNT(*):** To count rows: how many orders, how many pets per species.
- **COUNT(column):** To count rows that have a value in a column, or matches after a LEFT JOIN (count the right table's id).
- **COUNT(DISTINCT column):** To count different values: how many cities, how many customers ordered.

## Common mistakes

- `COUNT(*)` after a LEFT JOIN: unmatched rows count as 1, not 0. Count a column of the joined table.
- Expecting `COUNT(column)` to count NULLs: it skips them. Use `COUNT(*)` or `SUM(CASE WHEN column IS NULL THEN 1 ELSE 0 END)`.
- `COUNT(DISTINCT a, b)` with two columns: PostgreSQL and SQLite don't accept it. Count distinct pairs in a subquery.
