Difference between distinct and group by in SQL Server

Let’s have a look at difference between distinct and group by in SQL Server .  Distinct is used to find unique/distinct records where as a group by is used to group a selected set of rows into summary rows by one or more columns or an expression. The functional difference is thus obvious.

The group by can also be used to find distinct values as shown in below query.

1_difference between distinct and group by in sql server

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.

2_difference between distinct and group by in sql server

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.

Like us on FaceBook Join the fastest growing SQL Server group on FaceBook

Data Platform Virtual Summit 2020

Subscribe to SQLServerGeeks YouTube channel. If you want more learning content in your inbox, subscribe to SQLServerGeeks Bulletin.

SQLServerGeeks YouTube | SQLServerGeeks Bulletin | SQLServerGeeks Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *