Master SQL GROUP BY: From Simple Aggregations to Rollup and Joins
This guide explains how to use SQL's GROUP BY clause with examples covering basic grouping, GROUP_CONCAT, aggregate functions, HAVING filters, multi‑column grouping, WITH ROLLUP, and inner, left, and right joins, illustrating each technique with clear query results.
The GROUP BY clause groups rows that share the same values in specified columns, optionally filtered by HAVING and extended with WITH ROLLUP to add a summary row.
1. Basic GROUP BY
When used alone, the query returns one row per distinct group. select * from employee group by sex; This returns two rows, one for each gender.
2. GROUP BY with GROUP_CONCAT()
Combines values from each group into a comma‑separated list.
select sex, group_concat(name) from employee group by sex;Result example:
sex | group_concat(name)
女 | 小红, 小兰
男 | 张三, 王五, 王六3. GROUP BY with aggregate functions
Common aggregates include COUNT(), SUM(), AVG(), MAX(), and MIN().
select sex, count(sex) from employee group by sex;Result:
sex | count
女 | 1
男 | 34. Using HAVING with GROUP BY
HAVINGfilters groups after aggregation.
select sex, count(sex) from employee group by sex having count(sex) >= 3;Result shows only groups meeting the condition (e.g., male with count 3).
5. Grouping by multiple columns
select * from employee group by d_id, sex;The result is first grouped by d_id and then by sex within each department.
6. GROUP BY with WITH ROLLUP
WITH ROLLUPadds a final row containing totals for all groups.
select sex, count(sex) from employee group by sex with rollup;Result example:
sex | count
女 | 1
男 | 5
NULL| 67. Join queries
7.1 Inner join
select num, name, employee.d_id, age, d_name
from employee, department
where employee.d_id = department.d_id;Matches rows where the department IDs are equal.
7.2 Left join
select num, name, employee.d_id, age, d_name
from employee left join department on employee.d_id = department.d_id;Returns all rows from employee and matching rows from department; non‑matching department fields are NULL.
7.3 Right join
select num, name, employee.d_id, age, d_name
from employee right join department on employee.d_id = department.d_id;Returns all rows from department and matching rows from employee.
8. Common aggregate functions
COUNT(*)– counts rows; often used with GROUP BY. SUM(column) – sums numeric values; ignores non‑numeric fields. AVG(column) – calculates the average. MAX(column) and MIN(column) – return the highest and lowest values; for strings they compare ASCII codes.
These functions enable powerful data summarization when combined with GROUP BY and HAVING.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
