SQL COUNT UNIQUE: How to Count Distinct Values

Formulas & functions · Updated November 23, 2026

To count unique values in SQL, use COUNT(DISTINCT column_name). For example, SELECT COUNT(DISTINCT customer_id) FROM orders; returns the number of distinct customer IDs in the orders table.

What Does “Unique” Mean in SQL?

In SQL, “unique” refers to distinct values in a column—each value is counted only once, regardless of how many times it appears. This is different from counting all rows (COUNT(*)) or counting non-null values (COUNT(column)). For instance, if a table has 10 rows but only 3 different values in a column, COUNT(DISTINCT column) returns 3.

Note that NULL values are ignored by COUNT(DISTINCT). If a column contains NULL, it is not counted as a distinct value. This is consistent with standard SQL behavior across most databases.

Basic Syntax and Examples

The core syntax is straightforward:

SELECT COUNT(DISTINCT column_name) FROM table_name;

You can also count distinct combinations of multiple columns:

SELECT COUNT(DISTINCT column1, column2) FROM table_name;

This counts unique pairs of values from both columns. For example, if you have a table of product sales with product_id and region, COUNT(DISTINCT product_id, region) tells you how many unique product-region combinations exist.

You can also use it with conditions:

SELECT COUNT(DISTINCT customer_id) FROM orders WHERE order_date > '2024-01-01';

This counts distinct customers who placed orders after January 1, 2024.

COUNT DISTINCT vs. Other Counting Methods

It’s easy to confuse COUNT(DISTINCT) with other counting approaches. Here’s how they differ:

  • COUNT(*): Counts every row, including duplicates and NULLs.
  • COUNT(column): Counts non-NULL values in that column, but duplicates are counted multiple times.
  • COUNT(DISTINCT column): Counts unique non-NULL values only.

For example, with a column containing values (1, 1, 2, NULL), COUNT(*) returns 4, COUNT(column) returns 3, and COUNT(DISTINCT column) returns 2.

Performance Considerations and Alternatives

Counting distinct values can be resource-intensive on large tables because the database must sort or hash all values to identify duplicates. For tables with millions of rows, COUNT(DISTINCT) may be slow. If you need fast approximate counts, some databases offer approximate functions:

  • PostgreSQL: COUNT(DISTINCT) is exact; for approximate, use APPROX_COUNT_DISTINCT (available in PostgreSQL 14+ with the postgresql extension? Actually, it’s in some cloud versions; standard PostgreSQL doesn’t have it. Use HyperLogLog extensions).
  • MySQL: No built-in approximate distinct count; consider using APPROX_COUNT_DISTINCT in MySQL 8.0.17+? Actually, MySQL does not have it; use COUNT(DISTINCT) or a separate table.
  • SQL Server: COUNT(DISTINCT) is exact; for approximate, use APPROX_COUNT_DISTINCT (available in SQL Server 2019+).
  • BigQuery: COUNT(DISTINCT) is exact; use APPROX_COUNT_DISTINCT for faster approximate results.

If you need exact counts, be prepared for slower queries. Indexing the column can help, but even with an index, the database must scan all values.

Common Mistakes and Misconceptions

One common mistake is using COUNT(DISTINCT *)—this is not valid in most databases. You must specify a column or expression. Another mistake is expecting COUNT(DISTINCT) to count NULL as a value; it does not.

Another misconception: COUNT(DISTINCT column) and SELECT DISTINCT column followed by counting rows are equivalent, but the latter requires a subquery and is often less efficient. For example:

SELECT COUNT(*) FROM (SELECT DISTINCT column FROM table) AS sub;

This returns the same result but is more verbose and usually slower.

Also, be careful with data types. If a column stores numbers as text (e.g., ‘001’ vs ‘1’), they may be considered distinct even though they represent the same number. Casting can help: COUNT(DISTINCT CAST(column AS INT)).

Using COUNT DISTINCT with GROUP BY

You often need distinct counts per group. For example, to get the number of distinct products sold per region:

SELECT region, COUNT(DISTINCT product_id) AS unique_products FROM sales GROUP BY region;

This returns one row per region with the count of unique products sold there. This is a standard pattern for reporting.

You can also combine with HAVING to filter groups by their distinct count:

SELECT region, COUNT(DISTINCT product_id) AS unique_products FROM sales GROUP BY region HAVING COUNT(DISTINCT product_id) > 10;

Frequently Asked Questions

How do I count unique values in a column with NULLs?

COUNT(DISTINCT column) ignores NULLs. If you want to count NULL as a distinct value, use a workaround like COUNT(DISTINCT COALESCE(column, 'NULL_PLACEHOLDER'))—but be careful that the placeholder doesn’t collide with real values.

Can I use COUNT DISTINCT on multiple columns?

Yes, COUNT(DISTINCT col1, col2) counts distinct combinations. This works in most databases, but check your specific DBMS documentation as some older versions may not support multiple arguments.

What is the difference between COUNT(DISTINCT) and DISTINCT COUNT?

There is no “DISTINCT COUNT” in SQL. You might see COUNT(DISTINCT) or SELECT DISTINCT COUNT(column)—the latter counts all non-NULL values (duplicates included) and then removes duplicate counts, which is rarely what you want.

Why is COUNT(DISTINCT) slow on large tables?

Because the database must examine every row to find unique values, often requiring a sort or hash operation. For very large datasets, consider approximate functions or pre-aggregated summary tables.

Does COUNT(DISTINCT) work with text and date columns?

Yes, it works with any data type that supports equality comparison, including strings, dates, and numbers. For text, it counts distinct string values, case-sensitively depending on the database collation.

How do I count distinct values in Excel or Google Sheets?

In Excel, use =SUMPRODUCT((range<>"")/COUNTIF(range,range&"")) or the newer =COUNTA(UNIQUE(range)) in Excel 365. In Google Sheets, use =COUNTUNIQUE(range).

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.