MySQL COUNT(DISTINCT): Count Unique Values

Formulas & functions · Updated November 22, 2026

To count unique values in MySQL, use COUNT(DISTINCT column_name) in a SELECT statement. For example, SELECT COUNT(DISTINCT email) FROM users; returns the number of distinct email addresses in the users table.

What COUNT(DISTINCT) Actually Does

MySQL’s COUNT(DISTINCT expr) counts the number of rows where expr is not NULL, but only counts each distinct value once. It ignores NULL values entirely—if a column has 100 rows with 3 NULLs and 97 non-NULL values, COUNT(DISTINCT col) returns at most 97, not 100. The function works on a single expression, but you can pass multiple columns as arguments: COUNT(DISTINCT col1, col2) counts unique combinations of both columns. This is different from COUNT(DISTINCT col1) + COUNT(DISTINCT col2), which sums two separate counts.

For example, with a table of orders containing customer_id and product_id, COUNT(DISTINCT customer_id, product_id) counts how many distinct customer-product pairs exist. This is useful for finding unique combinations, like how many different customers bought each product.

Performance and Indexes

COUNT(DISTINCT) can be slow on large tables because MySQL must scan all rows and maintain a temporary set of seen values. The operation uses memory proportional to the number of unique values. For columns with a high cardinality (many distinct values), this can cause disk-based temporary tables, slowing queries significantly. Adding an index on the column can help, but MySQL still needs to read the index entries to count distinct values—it cannot just read index statistics because those are estimates, not exact counts.

For very large tables, consider using an approximate count with APPROX_COUNT_DISTINCT (available in MySQL 8.0.31 and later) which uses HyperLogLog and returns an estimate, trading accuracy for speed. The syntax is SELECT APPROX_COUNT_DISTINCT(col) FROM table;. This is ideal for dashboards where an exact number isn’t critical.

COUNT(DISTINCT) vs GROUP BY + COUNT

An alternative is to use GROUP BY and then count rows: SELECT COUNT(*) FROM (SELECT col FROM table GROUP BY col) AS t;. This is functionally equivalent to COUNT(DISTINCT col) for a single column. However, COUNT(DISTINCT) is often more readable and can be faster because it avoids creating a derived table. For multiple columns, GROUP BY col1, col2 then counting rows gives the same result as COUNT(DISTINCT col1, col2). The choice usually comes down to readability and whether you need other aggregated data from the same grouping.

If you also need other aggregates like SUM or AVG per group, GROUP BY is necessary. If you only need the unique count, COUNT(DISTINCT) is simpler.

Counting Unique Values with Conditions

You can combine COUNT(DISTINCT) with a WHERE clause to count unique values only among filtered rows. For instance, SELECT COUNT(DISTINCT user_id) FROM logins WHERE login_date >= '2024-01-01'; counts distinct users who logged in this year. You can also use HAVING after GROUP BY to filter groups based on their distinct counts, though that’s less common.

Another pattern is counting distinct values conditionally using SUM(CASE WHEN ...) but that doesn’t give distinct counts—it gives counts of rows matching a condition. To count distinct values that meet a condition, use a subquery: SELECT COUNT(DISTINCT col) FROM (SELECT col FROM table WHERE condition) AS t;.

Common Pitfalls and Misconceptions

A frequent mistake is assuming COUNT(DISTINCT col) includes NULLs—it doesn’t. If you need to count NULL as a distinct value, use COUNT(DISTINCT COALESCE(col, 'NULL_PLACEHOLDER')) but be careful with collisions. Another pitfall is using COUNT(DISTINCT) on expressions with floating-point numbers; due to rounding, values that appear equal may be counted as distinct. For example, COUNT(DISTINCT price * 0.1) might count 0.30000000000000004 and 0.3 as different.

Also, COUNT(DISTINCT) with a GROUP BY returns a count per group, not a total. If you want both per-group and overall, you need two queries or a window function like COUNT(DISTINCT col) OVER () (available in MySQL 8.0+).

Alternatives for Large Datasets

When exact counts are not required, APPROX_COUNT_DISTINCT is the best alternative. For exact counts on massive tables, consider maintaining a separate summary table that increments a counter when new unique values are inserted—this is common in data warehousing. Another approach is using a SET data type or a Bloom filter, but these are more advanced and not native to MySQL.

For real-time analytics, you might use a separate column that stores a hash of the value and count distinct hashes, but this has collision risks. Always test with your data volume to decide.

Frequently Asked Questions

Does COUNT(DISTINCT) count NULLs? No, it ignores NULL values. If you have 5 rows with values (1, 2, NULL, 2, 3), COUNT(DISTINCT col) returns 3, not 4.

Can I use COUNT(DISTINCT) with multiple columns? Yes, COUNT(DISTINCT col1, col2) counts unique combinations. Each combination is considered one distinct row.

Why is my COUNT(DISTINCT) query slow? It’s likely due to a large number of distinct values causing memory pressure. Add an index, use APPROX_COUNT_DISTINCT if approximate is fine, or filter with WHERE to reduce rows.

What’s the difference between COUNT(DISTINCT col) and COUNT(col)? COUNT(col) counts non-NULL rows, counting duplicates. COUNT(DISTINCT col) counts unique non-NULL values.

Does COUNT(DISTINCT) work with text or blob columns? Yes, but it may be slower and require more memory. For very large text, consider hashing first.

Is there a way to count distinct values per group? Yes, use SELECT group_col, COUNT(DISTINCT val) FROM table GROUP BY group_col;.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.