What is difference between DISTINCT and GROUP BY?


DISTINCT and GROUP BY both are used to remove duplicates. Performance is same during both queries. So best practice to use DISTINCT is when there is no Aggregate function involved in the query. If any aggregate function is involved than GROUP BY should be used.

Create table #Temp(
EmpName varchar(100),
Course Varchar(100)
)

Insert into #Temp
Select 'Neha','DotNet' union  
Select 'Bhawesh','PHP' union  
Select 'Abhi','PHP' union  
Select 'Hitesh','DotNet' union  
Select 'Priyanka','English'   


Select query with DISTINCT

Select Distinct EmpName,Course from #Temp

Distinct

Distinct

Select query with GROUP BY

Select Course,Count(1) as 'Count' from  #Temp group by Course

Group BY

Group BY

One thought on “What is difference between DISTINCT and GROUP BY?

Comments are closed.