65.9K
CodeProject is changing. Read more.
Home

COUNT of DISTINCT Rows in SQL Server

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.13/5 (7 votes)

Nov 24, 2010

CPOL
viewsIcon

80865

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:
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)