If knowledge is power, then sharing knowledge is ultimate powertrip
Latest Updates
You are here: Home >> 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

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top