SQL count(*) and distinct

sql

Solution

select count(*) from (select distinct * from MyTable) as T

Although I strongly suggest that you re-think any queries that use `DISTINCT`. In a large percentage of cases, `GROUP BY` is more appropriate (and faster).

EDIT: Having read the question comments, I should point out that you should never ask the DBMS to do more work than actually needs doing to get a result. If you know in advance that there will not be any duplicated rows in a table, then don't use `DISTINCT`.

Problem

Why can't we use `count(distinct *)` in SQL? As in to count all distinct rows?

Original source