MySQL Group By function is basically used to group the results depending on the given conditions.
In this tutorial we learn how to use Group By along with other functions in mysql.
Step 1 – Writing Query
In our database, there are multiple departments in the Department table, in which each employee is earning a different salary.
Suppose a scenario in which we want to see which department is getting salary more than $ 2500 per month, for this type of scenarios, we can’t simply use select queries, rather we have to make use of implicit functions in mysql.
For example in this case, we have to use mysql Group By clause, which will group the output according to the department number.
Next, to find out the salaries greater than 2500, we will use Having clause along with max function which would return the maximum salary.
The query which would be used for our database will be:
group by DepartmentID
having max(Salary) > 2500;
write this and press F5 to execute
We can see the output is now grouped according to the condition specified.
And that is how we can use Group By functions in mysql.