SQL FOUNDATIONS:GROUP BY & HAVING

Group rows and filter grouped results with HAVING.

SQL GROUP BY & HAVING Explained with Examples + Practice Questions

When to Use GROUP BY & HAVING

  • Use GROUP BY to group rows based on a column
  • Use HAVING to filter grouped results

These are essential for:

  • Data analysis
  • Reporting
  • SQL interviews (very high frequency)

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000
6AnkitIT52000

GROUP BY

GROUP BY groups rows that have the same values in a column.

Example: Count employees per department

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

Example: Average salary per department

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

Important Rule

Every column in SELECT must be:

  • Either in GROUP BY
  • Or used with an aggregate function

HAVING

HAVING is used to filter grouped results.

Example: Departments with more than 2 employees

sql
SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;

WHERE vs HAVING

ClauseUse
WHEREFilters rows before grouping
HAVINGFilters after grouping

Example Combining WHERE and HAVING

sql
SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE salary > 40000
GROUP BY department
HAVING AVG(salary) > 50000;

GROUP BY + ORDER BY

sql
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
ORDER BY total_salary DESC;

Key Concepts

  • GROUP BY groups data
  • HAVING filters grouped data
  • WHERE filters before grouping
  • Always use aggregate functions with GROUP BY
  • Common interview pattern

GROUP BY vs HAVING Summary

FeatureGROUP BYHAVING
PurposeGroup rowsFilter groups
Used withAggregationsAggregations
ExecutionBefore HAVINGAfter GROUP BY

Common Questions (FAQ)

What is GROUP BY in SQL

GROUP BY groups rows with the same values and applies aggregate functions.

What is HAVING in SQL

HAVING filters grouped results after aggregation.

Difference between WHERE and HAVING

WHERE filters rows before grouping, HAVING filters after grouping.

Can we use HAVING without GROUP BY

Yes, but it is uncommon and usually not needed.

Internal Learning Links

  • Learn Aggregations
  • Learn WHERE filtering
  • Learn ORDER BY

Practice CTA

Practice GROUP BY and HAVING problems on Schoolabe to master interview-level SQL queries.

GROUP BY & HAVING Missions

Solve exercises in sequence to unlock the next mission.

1

Count per department

Count employees in each department.

Solve Mission
2

Average per department

Find average salary for each department.

Locked
3

Total per department

Find total salary for each department.

Locked
4

Departments with >1

Find departments with more than 1 employee.

Locked
5

Avg salary > 55000

Find departments where average salary is greater than 55000.

Locked
6

Count salary > 50000

Count employees with salary greater than 50000 in each department.

Locked
7

Total salary > 100000

Find total salary of departments where total salary is greater than 100000.

Locked
8

Min salary > 45000

Find departments with minimum salary greater than 45000.

Locked
9

Sort by avg salary

Sort departments by average salary in descending order.

Locked
10

Highest total salary

Find department with highest total salary.

Locked