Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

COUNT of DISTINCT Rows in SQL Server

4.13/5 (7 votes)
26 Nov 2010CPOL 80.9K  
COUNT of DISTINCT Rows in SQL Server
SQL Server does not support COUNT(DISTINCT *). For example, the below query fails.

SELECT COUNT(DISTINCT *) FROM Employees


The error message generated is:

XML
Incorrect syntax near '*'


Instead, use the below query:

SELECT COUNT(*) FROM (SELECT DISTINCT * FROM Employees) Emp


Note: This is specifically useful when you want 'DISTINCT *' - without coloumn name(s)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)