๐Ÿ“Š GROUP BY & HAVING Clauses in SQL

๐ŸŸก Intermediate

๐Ÿ“– Definition

  • GROUP BY: An SQL clause that collapses individual table rows sharing identical values in specified columns into summary rows. It is paired with aggregate functions (COUNT, SUM, AVG, MIN, MAX) to compute metrics per category.
  • HAVING: An SQL clause used specifically to filter summarized groups after aggregation has occurred. It functions as the WHERE clause for grouped summary data.

---

๐Ÿ‡ฎ๐Ÿ‡ณ Hindi (Roman Script)

Data ko category-wise summarize karne ke liye GROUP BY clause ka use hota hai (jaise department-wise total salary ya city-wise customer count). WHERE aur HAVING mein antar samajhna sabse zaroori hai:

  • WHERE: Grouping hone se pehle individual raw rows ko filter karta hai. (Isme SUM ya AVG jaise aggregate functions nahi likh sakte).
  • HAVING: Grouping aur calculation hone ke baad summary groups ko filter karta hai (jaise HAVING SUM(sales) > 50000).

---

๐Ÿšฉ Marathi (Roman Script)

Data la category-wise summarize karnyasathi GROUP BY clause vaparla jato. WHERE aani HAVING madhil pharak samajne mahatvache ahe:

  • WHERE: Grouping honyapurvi raw rows la filter karto.
  • HAVING: Grouping aani aggregation zalyanantar summary groups la filter karto (jasakhi HAVING COUNT(*) >= 5).

---

๐Ÿค” Why Do We Use It?

In real-world business analytics, stakeholders need categorized summary reporting rather than raw transaction lists:

  • What is the total gross sales revenue per product category?
  • Which customers have placed 5 or more completed orders this month?
  • What is the average employee salary per department, excluding interns?

GROUP BY organizes raw rows into category buckets, and HAVING filters those buckets based on aggregate conditions.

---

๐Ÿง  Simple Analogy: Mailroom Sorting Trays

Think of GROUP BY and HAVING as a Postal Mailroom:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

โ”‚ Mailroom Operation โ”‚ SQL Clause Equivalent โ”‚

โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค

โ”‚ 1. Throwing away damaged envelopes โ”‚ WHERE status = 'VALID' (Pre-filter) โ”‚

โ”‚ 2. Sorting mail into City Trays โ”‚ GROUP BY city โ”‚

โ”‚ 3. Counting total letters in each trayโ”‚ Aggregate: COUNT(*) AS letter_count โ”‚

โ”‚ 4. Loading ONLY heavy trays (>50 msgs)โ”‚ HAVING COUNT(*) > 50 (Post-group filter) โ”‚

โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

โšก Logical Query Processing Order

Understanding the internal logical execution sequence of SQL clauses explains why aggregate conditions belong in HAVING rather than WHERE:

1. FROM & JOINs  โ”€โ”€โ–บ Locate source tables and join relationships

2. WHERE โ”€โ”€โ–บ Filter individual raw rows BEFORE grouping

3. GROUP BY โ”€โ”€โ–บ Partition remaining rows into category buckets

4. HAVING โ”€โ”€โ–บ Filter aggregate summary groups

5. SELECT โ”€โ”€โ–บ Project columns, evaluate aliases, compute aggregates

6. DISTINCT โ”€โ”€โ–บ Remove duplicate rows from final output

7. ORDER BY โ”€โ”€โ–บ Sort final output rows

8. LIMIT โ”€โ”€โ–บ Restrict maximum number of output rows

๐Ÿ’ก Why WHERE SUM(amount) > 100 Fails: In Step 2 (WHERE), individual rows are being evaluated one-by-one *before* groups exist! Aggregations do not exist until Step 3 (GROUP BY). Therefore, aggregate filter conditions must be placed in Step 4 (HAVING).

---

๐Ÿ” WHERE vs HAVING Comparison Matrix

FeatureWHERE ClauseHAVING Clause
Execution PointEvaluated BEFORE GROUP BY (filters raw rows).Evaluated AFTER GROUP BY (filters summary groups).
Aggregate FunctionsโŒ Prohibited (e.g. WHERE SUM(price) > 50 throws Error 1111).โœ… Allowed (e.g. HAVING SUM(price) > 50).
Column TargetsReferences raw table columns (department, status).References aggregate results (COUNT(*), AVG(salary)).
PerformanceHigh efficiency (reduces rows before grouping).Post-group overhead (groups rows before filtering).

Side-by-Side SQL Comparison:

-- Query A: Filter individual rows BEFORE grouping using WHERE

SELECT department_name, AVG(salary) AS avg_salary

FROM employees

WHERE status = 'Active' -- Filters out inactive employees first

GROUP BY department_name;

-- Query B: Filter aggregate summary groups AFTER grouping using HAVING

SELECT department_name, AVG(salary) AS avg_salary

FROM employees

GROUP BY department_name

HAVING AVG(salary) > 50000.00; -- Filters out departments with avg salary <= 50,000

---

๐Ÿ› ๏ธ Reproducible Example 1: Multi-Column GROUP BY & HAVING (sales_records)

Let's execute a complete MySQL-compatible workflow calculating annual revenue per department and filtering department years with $10,000+ in sales.

-- 1. Create sales_records table schema

CREATE TABLE IF NOT EXISTS sales_records (

order_id INT PRIMARY KEY,

department_name VARCHAR(50) NOT NULL,

order_date DATE NOT NULL,

status VARCHAR(20) NOT NULL, -- 'COMPLETED', 'PENDING', 'CANCELLED'

order_total DECIMAL(10, 2) NOT NULL

);

-- 2. Insert sample sales records

INSERT INTO sales_records (order_id, department_name, order_date, status, order_total)

VALUES

(1, 'Electronics', '2025-05-10', 'COMPLETED', 15000.00),

(2, 'Electronics', '2025-06-15', 'COMPLETED', 25000.00),

(3, 'Furniture', '2025-07-20', 'COMPLETED', 8000.00),

(4, 'Electronics', '2026-01-10', 'COMPLETED', 30000.00),

(5, 'Furniture', '2026-02-14', 'COMPLETED', 12000.00),

(6, 'Kitchen', '2026-03-01', 'COMPLETED', 4000.00),

(7, 'Electronics', '2026-03-10', 'PENDING', 5000.00);

Multi-Column Grouping Query:

SELECT 

department_name,

YEAR(order_date) AS order_year,

COUNT(order_id) AS completed_orders,

SUM(order_total) AS gross_revenue

FROM sales_records

WHERE status = 'COMPLETED' -- Step 1: Filter raw rows BEFORE grouping

GROUP BY department_name, YEAR(order_date) -- Step 2: Group by department AND year

HAVING SUM(order_total) >= 10000.00 -- Step 3: Filter groups generating $10,000+

ORDER BY gross_revenue DESC; -- Step 4: Sort results

๐Ÿ‘€ Expected Output:

+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+

department_nameorder_yearcompleted_ordersgross_revenue

+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+

Electronics2025240000.00
Electronics2026130000.00
Furniture2026112000.00

+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+

๐Ÿ” Query Breakdown:

  • WHERE status = 'COMPLETED': Filters out order #7 (PENDING) *before* grouping.
  • GROUP BY department_name, YEAR(order_date): Groups completed sales by both department name AND calendar year.
  • HAVING SUM(order_total) >= 10000.00: Excludes Furniture 2025 ($8000) and Kitchen 2026 ($4000) because their aggregate gross revenue is below $10,000.00.

---

๐Ÿ› ๏ธ Reproducible Example 2: VIP Customers (HAVING COUNT(*) >= 5)

Find high-frequency VIP customers who have placed 5 or more orders:

-- 1. Create orders table schema

CREATE TABLE IF NOT EXISTS orders (

order_id INT PRIMARY KEY,

customer_id INT NOT NULL,

total_amount DECIMAL(10, 2) NOT NULL

);

-- 2. Insert sample orders (Customer 1 & 3 have 5 orders; Customer 2 has 2)

INSERT INTO orders (order_id, customer_id, total_amount)

VALUES

(101, 1, 150.00), (102, 1, 200.00), (103, 1, 300.00), (104, 1, 100.00), (105, 1, 250.00),

(106, 2, 500.00), (107, 2, 600.00),

(108, 3, 100.00), (109, 3, 150.00), (110, 3, 200.00), (111, 3, 250.00), (112, 3, 300.00);

-- 3. Query VIP customers with 5+ orders

SELECT

customer_id,

COUNT(order_id) AS total_orders,

SUM(total_amount) AS total_spent,

ROUND(AVG(total_amount), 2) AS avg_order_value

FROM orders

GROUP BY customer_id

HAVING COUNT(order_id) >= 5

ORDER BY total_spent DESC;

๐Ÿ‘€ Expected Output:

+-------------+--------------+-------------+-----------------+

customer_idtotal_orderstotal_spentavg_order_value

+-------------+--------------+-------------+-----------------+

151000.00200.00
351000.00200.00

+-------------+--------------+-------------+-----------------+

*(Note: Customer 2 is excluded because they placed only 2 orders).*

---

๐Ÿ› ๏ธ Reproducible Example 3: Salary Audit Excluding Interns

Calculate average department salaries while excluding low-ranking interns before averaging:

-- 1. Create employees table schema

CREATE TABLE IF NOT EXISTS employees (

emp_id INT PRIMARY KEY,

first_name VARCHAR(50) NOT NULL,

department_name VARCHAR(50) NOT NULL,

job_title VARCHAR(50) NOT NULL,

salary DECIMAL(10, 2) NOT NULL

);

-- 2. Insert sample employees

INSERT INTO employees (emp_id, first_name, department_name, job_title, salary)

VALUES

(1, 'Rahul', 'Engineering', 'Senior Dev', 95000.00),

(2, 'Priya', 'Engineering', 'Junior Dev', 65000.00),

(3, 'Amit', 'Engineering', 'Intern', 20000.00),

(4, 'Sneha', 'HR', 'HR Manager', 70000.00),

(5, 'Kiran', 'HR', 'Intern', 18000.00),

(6, 'Vikram', 'Marketing', 'Lead Marketer', 85000.00);

-- 3. Department salary audit query

SELECT

department_name,

COUNT(emp_id) AS non_intern_count,

ROUND(AVG(salary), 2) AS avg_non_intern_salary

FROM employees

WHERE job_title != 'Intern' -- Exclude interns BEFORE calculating average

GROUP BY department_name

HAVING AVG(salary) >= 70000.00;

๐Ÿ‘€ Expected Output:

+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+

department_namenon_intern_countavg_non_intern_salary

+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+

Marketing185000.00
Engineering280000.00
HR170000.00

+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€+

๐Ÿ’ก Why WHERE job_title != 'Intern' Matters: If Amit's intern stipend ($20,000) was included in Engineering's average, the average would drop to $60,000. Excluing interns before grouping ensures the population average reflects non-intern staff ($80,000.00$).

---

โš ๏ธ MySQL ONLY_FULL_GROUP_BY & Common Mistakes

Error / PitfallCauseRecommended Fix
ERROR 1111 (HY000): Invalid use of group functionWriting aggregate functions inside WHERE (e.g. WHERE SUM(sales) > 100).Move aggregate conditions to HAVING SUM(sales) > 100.
ERROR 1055 (42000): ONLY_FULL_GROUP_BY violationSelecting a non-aggregated column in SELECT that is NOT listed in GROUP BY.Include all non-aggregated SELECT columns in the GROUP BY clause.
Using HAVING for Non-Aggregate Row FilteringWriting HAVING status = 'COMPLETED' instead of WHERE status = 'COMPLETED'.Use WHERE for row filtering. WHERE filters rows before grouping, improving performance.
Expecting HAVING Aliases in WHEREReferencing column aliases created in SELECT inside the WHERE clause.WHERE executes before SELECT aliases exist. Use column names in WHERE.

---

๐ŸŒ Real-World Usage

  • E-Commerce VIP Loyalty Programs: Identifying top customers spending over $1,000 across 5+ completed orders (HAVING SUM(total_amount) > 1000).
  • Financial Audit Systems: Identifying department branches exceeding monthly operational budgets.
  • Inventory Logistics: Grouping product stock by warehouse location and alerting when regional inventory falls below safety thresholds.

---

๐Ÿงช Learning Exercises & Self-Checks

Exercise 1: Students Table Setup & Grouping

Given the following students table:

CREATE TABLE IF NOT EXISTS students (

student_id INT PRIMARY KEY,

student_name VARCHAR(50) NOT NULL,

grade_level VARCHAR(20) NOT NULL,

tuition_fee DECIMAL(10, 2) NOT NULL

);

INSERT INTO students (student_id, student_name, grade_level, tuition_fee)

VALUES

(1, 'Rahul', 'Grade 10', 6000.00),

(2, 'Priya', 'Grade 10', 6000.00),

(3, 'Amit', 'Grade 10', 6000.00),

(4, 'Sneha', 'Grade 11', 7500.00),

(5, 'Kiran', 'Grade 11', 7500.00),

(6, 'Neha', 'Grade 12', 9000.00);

1. Write a query counting total students per grade_level.

2. Write a query displaying only grade levels that have more than 2 students (HAVING COUNT(*) > 2).

3. Write a query calculating total tuition fees per grade level for students paying tuition_fee >= 7000.00.

---

๐ŸŽฏ Mini Challenge

1. Create an e_commerce_orders table schema:

  • order_id (INT, Primary Key)
  • customer_id (INT)
  • order_status (VARCHAR 20 โ€” 'DELIVERED', 'PENDING', 'CANCELLED')
  • item_count (INT)
  • total_price (DECIMAL 10,2)

2. Insert sample orders:

  • Order 1: Cust 1, 'DELIVERED', 5 items, $300.00
  • Order 2: Cust 1, 'DELIVERED', 6 items, $250.00
  • Order 3: Cust 2, 'DELIVERED', 12 items, $600.00
  • Order 4: Cust 2, 'CANCELLED', 10 items, $500.00
  • Order 5: Cust 3, 'DELIVERED', 2 items, $100.00

3. Write a query that:

  • Filters completed orders (WHERE order_status = 'DELIVERED').
  • Groups by customer_id.
  • Displays customer_id, total items purchased (SUM(item_count)), and total spend (SUM(total_price)).
  • Filters using HAVING to retain only customers whose total spend exceeds $500.00 AND whose total item count is at least 10 items (HAVING SUM(total_price) > 500.00 AND SUM(item_count) >= 10).

๐Ÿ” Click to view Exercise Solutions & Answer Key

Answer Key:

Exercise 1 Solutions:

-- Task 1: Count students per grade level

SELECT grade_level, COUNT(*) AS student_count

FROM students

GROUP BY grade_level;

-- Task 2: Grade levels with > 2 students

SELECT grade_level, COUNT(*) AS student_count

FROM students

GROUP BY grade_level

HAVING COUNT(*) > 2;

-- Output: Grade 10 (3 students)

-- Task 3: Total tuition fees per grade level for fees >= 7000.00

SELECT grade_level, SUM(tuition_fee) AS total_tuition

FROM students

WHERE tuition_fee >= 7000.00

GROUP BY grade_level;

-- Output: Grade 11 ($15,000.00), Grade 12 ($9,000.00)

Mini Challenge Solution:

-- Step 1: Create table schema

CREATE TABLE IF NOT EXISTS e_commerce_orders (

order_id INT PRIMARY KEY,

customer_id INT NOT NULL,

order_status VARCHAR(20) NOT NULL,

item_count INT NOT NULL,

total_price DECIMAL(10, 2) NOT NULL

);

-- Step 2: Insert sample orders

INSERT INTO e_commerce_orders (order_id, customer_id, order_status, item_count, total_price)

VALUES

(1, 1, 'DELIVERED', 5, 300.00),

(2, 1, 'DELIVERED', 6, 250.00),

(3, 2, 'DELIVERED', 12, 600.00),

(4, 2, 'CANCELLED', 10, 500.00),

(5, 3, 'DELIVERED', 2, 100.00);

-- Step 3: Execute GROUP BY + HAVING query

SELECT

customer_id,

SUM(item_count) AS total_items,

SUM(total_price) AS total_spend

FROM e_commerce_orders

WHERE order_status = 'DELIVERED'

GROUP BY customer_id

HAVING SUM(total_price) > 500.00

AND SUM(item_count) >= 10;

Expected Output:

+-------------+-------------+-------------+

customer_idtotal_itemstotal_spend

+-------------+-------------+-------------+

111550.00
212600.00

+-------------+-------------+-------------+

*(Explanation: Customer 1 has 11 delivered items spending $550.00. Customer 2 has 12 delivered items spending $600.00 [order #4 was CANCELLED so it is filtered out by WHERE]. Customer 3 is excluded because they bought only 2 items spending $100.00).*

---

๐Ÿ”— Related Topics

---

๐Ÿงญ Navigation

โ† Previous: Aggregate Functions | SQL Home | Next: SQL Joins โ†’