Group BY
In MySQL, the GROUP BY statement is for applying an association on the aggregate functions for a group of the result-set with one or more columns. Group BY is very useful for fetching information about a group of data. If we have only one product of each type, then GROUP BY
would not be all that useful.
The GROUP BY statement only shows when you have many similar things.
For example: If we have a number of products of the same type, and we
want to find out some statistical information like the minimum, maximum, or
other top-level info, we would use GROUP BY. The column that you GROUP BY must
also be in your SELECT statement. Remember to group by the column you want
information about and not the one you are applying the aggregate function on.
Example : With the help of the 'emp_information'
table explain the GROUP BY statement.
For Example : With the use of the
select statement apply the GROUP BY statement.
COUNT() and GROUP BY
Example : Select emp_salary and count the number of emp_name in the "emp_information"
table.
It seems that the GROUP BY clause
only scans for unique occurrences in the status
column and return the result set.
Example : select emp_salary from the emp_information table and show
only unique column.
MySQL also allows we to sort the
group order in which the results are returned. The default order is ascending.If i want to see the result of the query above in the descending order, i can
do it as follows.
Example : The given example shows that emp_salary in desc form.
GROUP BY with order and HAVING :
Example : In the given example we have to use the group by with order and having on the 'sales' table.