Grouping
Grouping
md 2024-09-04
We'll explore these concepts in detail using the Employees and Departments tables, accompanied by several
practical examples.
Tables Overview
Employees Table
The Employees table contains information about employees, including their department, salary, and join date.
Departments Table
The Departments table contains information about various departments and their locations.
1 / 10
Grouping.md 2024-09-04
GROUP BY Clause
Theory
The GROUP BY clause is a powerful tool in SQL used to organize data into distinct groups. By grouping rows
that have identical values in specified columns, you can apply aggregate functions like COUNT, SUM, AVG, MAX,
2 / 10
Grouping.md 2024-09-04
and MIN to these groups. This allows for concise data summaries and is particularly useful for reporting and
data analysis.
Explanation:
SELECT Department: We are grouping the data based on the Department column.
COUNT(*): Counts the number of employees in each department.
GROUP BY Department: Groups the rows by the Department column.
Result:
To understand how much each department spends on salaries, we can sum up the salaries of all employees
within each department.
Explanation:
Result:
Explanation:
4 / 10
Grouping.md 2024-09-04
Result:
To analyze the number of employees who joined each department in a particular year, we can use the YEAR()
function to extract the year from the JoinDate column.
Explanation:
Result:
5 / 10
Grouping.md 2024-09-04
HAVING Clause
Theory
The HAVING clause allows you to filter groups of data created by the GROUP BY clause. While the WHERE clause
filters rows before they are grouped, HAVING filters the groups themselves based on aggregate functions. This
is especially useful for refining your results after performing calculations like averages, totals, and counts.
Financial Compliance: Filter departments with average salaries below a certain threshold.
Sales Analysis: Identify sales teams that have underperformed based on total sales.
Operational Efficiency: Highlight departments with fewer than a specified number of employees.
We can use the HAVING clause to list departments that have more than 2 employees.
Explanation:
HAVING COUNT(*) > 2: Filters departments where the count of employees is greater than 2.
6 / 10
Grouping.md 2024-09-04
Result:
Explanation:
HAVING SUM(Salary) > 150000.00: Filters departments where the total salary is greater than
$150,000.
Result:
7 / 10
Grouping.md 2024-09-04
To focus on departments where the average salary is below $55,000, use the following query:
Explanation:
HAVING AVG(Salary) < 55000.00: Filters departments where the average salary is less than $55,000.
Result:
8 / 10
Grouping.md 2024-09-04
To list departments that have employees who joined after 2020 and have more than one employee, you can
combine conditions using HAVING.
Explanation:
WHERE YEAR(JoinDate) > 2020: Filters employees who joined after 2020.
HAVING COUNT(*) > 1: Filters departments with more than one such employee.
Result:
9 / 10
Grouping.md 2024-09-04
Conclusion
Understanding the GROUP BY and HAVING clauses is essential for any SQL practitioner, as they allow you to
efficiently aggregate and filter data. These clauses are powerful tools for generating meaningful insights from
your data, whether for business analytics, financial reporting, or operational efficiency.
10 / 10