π SQL Grouping & Filtering β GROUP BY and HAVING Explained
π§² Introduction β Why Learn SQL Grouping & Filtering?
When analyzing large datasets, it’s often essential to group similar records and apply filters on aggregated data. SQL provides two powerful clauses to do this:
GROUP BY: Groups rows sharing a common valueHAVING: Filters the results after aggregation
Mastering these clauses allows you to:
- π§ Aggregate metrics like COUNT, SUM, AVG
- π Organize data into meaningful categories
- π Apply filters to grouped results (unlike WHERE)
π― In this tutorial, youβll learn how to:
- Group data using
GROUP BY - Filter grouped data using
HAVING - Combine both for powerful reporting queries
π Topics Covered
| π Topic | π Description |
|---|---|
| π₯ SQL GROUP BY | Organize results into categories for aggregation |
| π SQL HAVING | Apply conditions on grouped and aggregated data |
π₯ SQL GROUP BY β Categorize and Aggregate Data
The GROUP BY clause is used to group rows based on one or more columns. Itβs commonly used with aggregate functions like SUM(), AVG(), COUNT(), etc.
β Syntax:
SELECT column1, AGGREGATE_FUNCTION(column2)
FROM table_name
GROUP BY column1;
π Example β Total Salary by Department:
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
β This groups employees by department and calculates the total salary per group.
π SQL HAVING β Filter Aggregated Results
The HAVING clause works like WHERE, but it applies after the GROUP BY has aggregated data.
β Syntax:
SELECT column1, AGGREGATE_FUNCTION(column2)
FROM table_name
GROUP BY column1
HAVING condition;
π Example β Departments with Total Salary > 100,000:
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
HAVING SUM(salary) > 100000;
β This filters out departments where the total salary is β€ 100,000.
βοΈ GROUP BY vs HAVING vs WHERE β Key Differences
| Clause | Use Case | Applies To |
|---|---|---|
WHERE | Filters before aggregation | Individual rows |
GROUP BY | Groups rows based on column values | Raw rows |
HAVING | Filters after aggregation | Grouped data |
π§ͺ Example β GROUP BY + HAVING in Action
SELECT department, COUNT(*) AS employee_count
FROM employees
WHERE status = 'active'
GROUP BY department
HAVING COUNT(*) >= 5;
π This query:
- Filters out inactive employees
- Groups active employees by department
- Returns only departments with 5 or more active employees
π Best Practices for GROUP BY and HAVING
| β Best Practices | π« Common Mistakes |
|---|---|
Always use aggregate functions with HAVING | Donβt use HAVING like WHERE for raw data |
Use AS to label your result columns | Avoid ambiguous or default column names |
Use WHERE for row-level filtering | Donβt skip GROUP BY when aggregating multiple groups |
| Ensure grouped column is in SELECT | Avoid syntax errors by mismatching grouped and selected columns |
π Summary β Recap & Next Steps
SQLβs GROUP BY and HAVING clauses let you analyze, categorize, and filter data with precision. They are fundamental for data analysis, reporting, and summarizing datasets.
π Key Takeaways:
- Use
GROUP BYto organize data for aggregation - Use
HAVINGto filter groups after aggregation - Combine with aggregate functions for insights like totals, averages, and counts
βοΈ Real-World Relevance:
These clauses are widely used in analytics dashboards, financial reports, performance metrics, and customer segmentation queries.
β‘οΈ Next Up: Explore 𧬠SQL Subqueries & Nested Queries to build more dynamic and layered logic.
β FAQs β SQL Grouping & Filtering
β Can I use HAVING without GROUP BY?
β
Technically yes, but it’s only meaningful when you’re using aggregate functions. Otherwise, use WHERE.
β Whatβs the difference between WHERE and HAVING?
β
WHERE filters before grouping; HAVING filters after aggregation.
β Can I group by multiple columns?
β
Yes, just separate them with commas:
GROUP BY department, location;
β Can I use aliases in HAVING?
β
Not in all databases. Itβs safer to repeat the aggregate expression in HAVING.
β What aggregate functions can be used with GROUP BY?
β
COUNT(), SUM(), AVG(), MIN(), MAX(), and even nested functions like ROUND(AVG(salary), 2).
Share Now :
