The group by the statement in MySQL groups rows that have the same values into orderly rows. Group by is often used with aggregate functions such as count, sum, max, min, and avg to place it and group the result set by one or more columns.
Group By Syntax MySQL
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s);
MySQL Group By Example
Let us try to group something using this clause. For this, we will be using the film table.
Let us try to group by title name.
SELECT COUNT(film_id), title
FROM film
GROUP BY title;
This query fetches it for the count of the film ids and groups by title name in an ordered fashion.
Conclusion:
So this is how we can use the group by clause one way, and there are other ways we can couple up the group by with like join clause, etc.
- Log in to post comments