SQL GROUP BY with HAVING Clause - Detailed Guide
1. What is GROUP BY?
The GROUP BY clause is used to group rows with the same values into summary rows.
It is used with aggregate functions like SUM(), COUNT(), AVG(), etc.
2. What is HAVING?
The HAVING clause is used to filter groups after the GROUP BY operation.
It is similar to WHERE, but WHERE is used before grouping, and HAVING is used after grouping.
3. Sample Table: Orders
CREATE TABLE Orders (
order_id INT,
customer_id INT,
product VARCHAR(100),
order_date DATE,
quantity INT,
price DECIMAL(10, 2)
);
Sample Data (only a few shown out of thousands):
(101, 1, 'Laptop', '2024-01-10', 2, 1200.00),
(102, 1, 'Mouse', '2024-01-11', 1, 25.00),
(103, 2, 'Laptop', '2024-01-15', 1, 1200.00),
(104, 3, 'Keyboard', '2024-02-05', 1, 45.00),
(105, 2, 'Monitor', '2024-02-10', 2, 300.00),
(106, 3, 'Mouse', '2024-03-01', 3, 25.00),
(107, 1, 'Laptop', '2024-03-05', 1, 1200.00),
(108, 2, 'Mouse', '2024-03-15', 2, 25.00),
(109, 4, 'Laptop', '2024-03-20', 1, 1200.00);
SQL GROUP BY with HAVING Clause - Detailed Guide
4. Example: Total Sales Per Customer
SELECT customer_id, SUM(quantity * price) AS total_sales
FROM Orders
GROUP BY customer_id;
This gives total sales per customer.
5. Example: Customers Who Spent More Than 2000
SELECT customer_id, SUM(quantity * price) AS total_sales
FROM Orders
GROUP BY customer_id
HAVING SUM(quantity * price) > 2000;
Filters out customers whose total sales are <= 2000.
6. Example: Count of Orders Per Product (With Filter)
SELECT product, COUNT(order_id) AS total_orders
FROM Orders
GROUP BY product
HAVING COUNT(order_id) >= 2;
7. GROUP BY vs HAVING vs WHERE
| Clause | Works On | When Applied | Example Use |
|----------|--------------|----------------------|---------------------------|
| WHERE | Rows | Before Grouping | WHERE price > 100 |
SQL GROUP BY with HAVING Clause - Detailed Guide
| GROUP BY | Groups | During Grouping | GROUP BY customer_id |
| HAVING | Groups | After Grouping | HAVING SUM(price) > 500 |
8. Best Practices for Large Data
- Use WHERE to filter early.
- Create indexes on columns used in WHERE and GROUP BY.
- Avoid using HAVING as WHERE.
- Use CTEs for complex filters.
- Use aliases for clarity.
9. Summary
GROUP BY is used to summarize data by column values.
HAVING is used to filter the summarized data.
Always use WHERE before grouping and HAVING after grouping.