SQL to find the number of distinct values in a column

SqlDistinct

Sql Problem Overview


I can select all the distinct values in a column in the following ways:

  • SELECT DISTINCT column_name FROM table_name;
  • SELECT column_name FROM table_name GROUP BY column_name;

But how do I get the row count from that query? Is a subquery required?

Sql Solutions


Solution 1 - Sql

You can use the DISTINCT keyword within the COUNT aggregate function:

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

This will count only the distinct values for that column.

Solution 2 - Sql

This will give you BOTH the distinct column values and the count of each value. I usually find that I want to know both pieces of information.

SELECT [columnName], count([columnName]) AS CountOf
FROM [tableName]
GROUP BY [columnName]

Solution 3 - Sql

Be aware that Count() ignores null values, so if you need to allow for null as its own distinct value you can do something tricky like:

select count(distinct my_col)
       + count(distinct Case when my_col is null then 1 else null end)
from my_table
/

Solution 4 - Sql

An sql sum of column_name's unique values and sorted by the frequency:

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name ORDER BY 2 DESC;

Solution 5 - Sql

SELECT COUNT(DISTINCT column_name) FROM table as column_name_count;

you've got to count that distinct col, then give it an alias.

Solution 6 - Sql

select count(*) from 
(
SELECT distinct column1,column2,column3,column4 FROM abcd
) T

This will give count of distinct group of columns.

Solution 7 - Sql

select Count(distinct columnName) as columnNameCount from tableName 

Solution 8 - Sql

Using following SQL we can get the distinct column value count in Oracle 11g.

select count(distinct(Column_Name)) from TableName

Solution 9 - Sql

After MS SQL Server 2012, you can use window function too.

SELECT column_name, COUNT(column_name) OVER (PARTITION BY column_name) 
FROM table_name
GROUP BY column_name

Solution 10 - Sql

To do this in Presto using OVER:

SELECT DISTINCT my_col,
                count(*) OVER (PARTITION BY my_col
                               ORDER BY my_col) AS num_rows
FROM my_tbl

Using this OVER based approach is of course optional. In the above SQL, I found specifying DISTINCT and ORDER BY to be necessary.

Caution: As per the docs, using GROUP BY may be more efficient.

Solution 11 - Sql

select count(distinct(column_name)) AS columndatacount from table_name where somecondition=true

You can use this query, to count different/distinct data.

Solution 12 - Sql

Count(distinct({fieldname})) is redundant

Simply Count({fieldname}) gives you all the distinct values in that table. It will not (as many presume) just give you the Count of the table [i.e. NOT the same as Count(*) from table]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristian OudardView Question on Stackoverflow
Solution 1 - SqlNoah GoodrichView Answer on Stackoverflow
Solution 2 - SqlPaul JamesView Answer on Stackoverflow
Solution 3 - SqlDavid AldridgeView Answer on Stackoverflow
Solution 4 - SqlxchiltonxView Answer on Stackoverflow
Solution 5 - SqlPete Karl IIView Answer on Stackoverflow
Solution 6 - SqlVaibhavView Answer on Stackoverflow
Solution 7 - SqlWayneView Answer on Stackoverflow
Solution 8 - SqlNilesh ShindeView Answer on Stackoverflow
Solution 9 - SqlAlperView Answer on Stackoverflow
Solution 10 - SqlAsclepiusView Answer on Stackoverflow
Solution 11 - SqlNitika ChopraView Answer on Stackoverflow
Solution 12 - SqlPaul PenaView Answer on Stackoverflow