Count distinct values and tuples with explicit NULL semantics

COUNT(*) counts rows. COUNT(Column) counts rows where that expression is not NULL. COUNT(DISTINCT Column) counts distinct non-null values. These are different questions, even when a small dataset happens to produce the same answer.

For distinct combinations of several columns, a subquery makes the chosen row shape explicit. It also avoids concatenating values into a string whose delimiters, type conversions or missing values can change the identity being counted.

Compare the three single-column counts

Run this complete read-only fixture. It contains six rows, including one repeated pair and two NULL codes.

SQL
WITH Items(Team, Code) AS (
    SELECT 'A', 'bolt'
    UNION ALL SELECT 'A', 'bolt'
    UNION ALL SELECT 'A', 'nut'
    UNION ALL SELECT 'A', NULL
    UNION ALL SELECT 'B', 'bolt'
    UNION ALL SELECT 'B', NULL
)
SELECT COUNT(*) AS AllRows,
       COUNT(Code) AS RowsWithCode,
       COUNT(DISTINCT Code) AS DistinctCodes
FROM Items;

The result is AllRows = 6, RowsWithCode = 4 and DistinctCodes = 2. bolt and nut are the two non-null codes. Repeated bolt values still contribute to COUNT(Code), but contribute only once to the distinct count.

Adding DISTINCT outside an aggregate, as in SELECT DISTINCT COUNT(*), removes duplicate aggregate result rows. It does not change which input rows that count consumed. Put the distinct operation at the level that expresses the intended identity.

Count distinct pairs explicitly

SQL
WITH Items(Team, Code) AS (
    SELECT 'A', 'bolt'
    UNION ALL SELECT 'A', 'bolt'
    UNION ALL SELECT 'A', 'nut'
    UNION ALL SELECT 'A', NULL
    UNION ALL SELECT 'B', 'bolt'
    UNION ALL SELECT 'B', NULL
)
SELECT COUNT(*) AS DistinctPairs
FROM (
    SELECT DISTINCT Team, Code
    FROM Items
) AS Pairs;

This returns 5. The distinct pairs are A/bolt, A/nut, A/NULL, B/bolt and B/NULL. The outer COUNT(*) includes every row produced by the subquery, including the pairs whose code is NULL.

If pairs with missing codes are ineligible, add WHERE Code IS NOT NULL inside the distinct subquery. The result becomes 3. State that exclusion rather than letting a dialect-specific shorthand make the decision implicitly.

Count within each group

SQL
WITH Items(Team, Code) AS (
    SELECT 'A', 'bolt'
    UNION ALL SELECT 'A', 'bolt'
    UNION ALL SELECT 'A', 'nut'
    UNION ALL SELECT 'A', NULL
    UNION ALL SELECT 'B', 'bolt'
    UNION ALL SELECT 'B', NULL
)
SELECT Team, COUNT(DISTINCT Code) AS DistinctCodes
FROM Items
GROUP BY Team
ORDER BY Team;

Team A has 2 distinct non-null codes and team B has 1. The group key defines where counting restarts. Summing those counts gives 3, which is not the global distinct count of 2 because bolt belongs to both teams.

A grouped query returns only groups present in its input. If a report must include teams with no items, start from the team relation and join deliberately. A COUNT(*) over a left join can count the placeholder row, so choose an expression whose NULL behavior matches the intended measure.

Recognize dialect shortcuts

SQL Server's aggregate accepts one expression after DISTINCT. It does not accept MySQL's multi-expression form COUNT(DISTINCT Team, Code). In MySQL, that form also excludes combinations containing NULL, which would yield 3 for this fixture rather than the subquery's 5. The explicit distinct-subquery pattern keeps that choice visible across these engines.

Do not emulate tuple identity with Team + Code or a delimiter-based concatenation. Different pairs can map to the same text, and NULL propagation or collation can introduce further differences. Compare the actual typed columns.

SQL Server returns int from COUNT, so use COUNT_BIG when the possible result exceeds that range. On large datasets, distinct aggregation may require substantial work. Inspect indexes and execution plans after establishing the correct NULL, grouping and identity rules.

Source and further reading

This article addresses the problem raised by somacore in “Selecting COUNT(*) with DISTINCT” on Stack Overflow, licensed under CC BY-SA 4.0 . The question is the topic source. The explanations and examples here are newly authored from the database documentation, rather than reproduced from a community answer.

COUNT (Transact-SQL) - SQL Server
COUNT (Transact-SQL)
https://learn.microsoft.com/en-us/sql/t-sql/functions/count-transact-sql?view=sql-server-ver17
SELECT Clause (Transact-SQL) - SQL Server
The SELECT clause specifies the columns that the Transact-SQL query returns.
https://learn.microsoft.com/en-us/sql/t-sql/queries/select-clause-transact-sql?view=sql-server-ver17
MySQL :: MySQL 8.4 Reference Manual :: 14.19.1 Aggregate Function Descriptions
https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html

Author: Herman Schoenfeld