Difference Between GROUP BY and DISTINCT

The two queries return same result. The group by gives the same result as of distinct when no aggregate function is present. The SQL Server query optimizer produces the same plan for both the queries as shown below.

Thus, to conclude there is a functional difference as mentioned above even if the group by produces same result as of distinct. Group By operator is meant for aggregating/grouping rows where as distinct is just used to get distinct values.
The most important difference in my opinion is the fact that the two operations "happen" at two very different steps in the logical order of operations that are executed in a SELECT statement.
Here are the most important operations:
FROM(includingJOIN,APPLY, etc.)WHEREGROUP BY(can remove duplicates)- Aggregations
HAVING- Window functions
SELECTDISTINCT(can remove duplicates)UNION,INTERSECT,EXCEPT(can remove duplicates)ORDER BYOFFSETLIMIT
As you can see, the logical order of each operation influences what can be done with it and how it influences subsequent operations. In particular, the fact that the GROUP BY operation "happens before" the SELECT operation (the projection) means that:
- It doesn't depend on the projection (which can be an advantage)
- It cannot use any values from the projection (which can be a disadvantage)
Ref:
https://sqlservergeeks.com/difference-between-distinct-and-group-by-in-sql-server/
Post a Comment
0 Comments