If knowledge is power, then sharing knowledge is ultimate powertrip
PMP®, SAFe®5 POPM, SAFe®5 SSM, SAFe®5 SA, Prince2, MCP
Latest Updates
You are here: Home > Databases > MySql > Difference between distinct and group by in mysql

Difference between distinct and group by in mysql

The difference between DISTINCT and GROUP BY in Mysql is that they usually generate the same query plan, so performance should be the same across both query constructs.

GROUP BY should be used to apply aggregate operators to each group. If all you need is to remove duplicates then use DISTINCT. If you are using sub-queries execution plan for that query varies so in that case you need to check the execution plan before making decision of which is faster.

Example of DISTINCT:
SELECT DISTINCT Employee, Dept FROM Employees

Example of GROUP BY:
SELECT Employee, Dept FROM Employees GROUP BY Employee, Dept

Example of GROUP BY with aggregate function:
SELECT Employee, Dept, COUNT(*) EmployeeCount FROM Employees GROUP BY Employee, Dept

 

About Jawed Shamshedi

Scroll To Top