TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2723 1.21K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 6

GROUP BY & HAVING โ€” Analyzing Data by Categories ๐Ÿ“Š

In Part 5, you learned how aggregate functions answer questions like:



What is the total revenue?

How many customers do we have?



But real-world business questions are usually more specific:



What is the revenue by city?

How many employees are there in each department?

Which products generated the most revenue?



That's where GROUP BY comes in.

1๏ธโƒฃ What is GROUP BY?

GROUP BY combines rows with the same value into groups so that aggregate functions can calculate a metric for each group.

Basic Syntax

SELECT
column_name,
aggregate_function(column)
FROM table_name
GROUP BY column_name;


Example:

SELECT
department,
COUNT(*) AS employee_count
FROM employees
GROUP BY department;


Instead of getting one total employee count, you get a count for each department.

2๏ธโƒฃ Why Do We Need GROUP BY?

Without GROUP BY:

SELECT COUNT(*) AS total_employees
FROM employees;


Result: 1000

This answers: How many employees are there?

But:

SELECT
department,
COUNT(*) AS employee_count
FROM employees
GROUP BY department;


Result:

โ€ข IT | 350

โ€ข Finance | 200

โ€ข HR | 120

โ€ข Sales | 330

Now you can answer: How many employees are in each department?

3๏ธโƒฃ GROUP BY With COUNT()

This is probably the most common GROUP BY pattern.

SELECT
city,
COUNT(*) AS customer_count
FROM customers
GROUP BY city;


4๏ธโƒฃ GROUP BY With SUM()

Suppose you want revenue by city.

SELECT
city,
SUM(amount) AS total_revenue
FROM orders
GROUP BY city;


This is a common business KPI.

5๏ธโƒฃ GROUP BY With AVG()

Calculate average salary by department:

SELECT
department,
AVG(salary) AS average_salary
FROM employees
GROUP BY department;


6๏ธโƒฃ GROUP BY With MIN() and MAX()

You can use multiple aggregate functions.

SELECT
department,
MIN(salary) AS minimum_salary,
MAX(salary) AS maximum_salary,
AVG(salary) AS average_salary
FROM employees
GROUP BY department;


7๏ธโƒฃ Multiple Aggregations

You aren't limited to one metric.

SELECT
department,
COUNT(*) AS employees,
SUM(salary) AS total_salary,
AVG(salary) AS average_salary,
MIN(salary) AS minimum_salary,
MAX(salary) AS maximum_salary
FROM employees
GROUP BY department;


This is the foundation of many analytical reports.

8๏ธโƒฃ GROUP BY Multiple Columns

You can group by more than one column.

Example: Count customers by city and customer segment.

SELECT
city,
customer_segment,
COUNT(*) AS customer_count
FROM customers
GROUP BY
city,
customer_segment;


SQL creates a group for each unique combination.

9๏ธโƒฃ Understanding Multiple GROUP BY Columns

Grouping by GROUP BY city, segment creates groups like:

โ€ข Mumbai + Premium

โ€ข Mumbai + Standard

โ€ข Delhi + Premium

The combination matters.

๐Ÿ”Ÿ GROUP BY With WHERE

WHERE filters rows before grouping.

Example: Calculate revenue by city for completed orders only.

SELECT
city,
SUM(amount) AS revenue
FROM orders
WHERE order_status = 'Completed'
GROUP BY city;


Conceptually: All Orders โ†’ WHERE Completed โ†’ GROUP BY City โ†’ SUM Revenue

1๏ธโƒฃ1๏ธโƒฃ WHERE vs GROUP BY

WHERE Answers: Which rows should be included?

GROUP BY Answers: How should those rows be divided into groups?

1๏ธโƒฃ2๏ธโƒฃ What is HAVING?

HAVING filters groups after aggregation.

Example: Find departments with more than 100 employees.
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’