SQL / SQL Aggregation and Grouping
Filtering Groups with HAVING
Our next tutorial will cover the HAVING clause in SQL. This clause allows you to filter the results of a GROUP BY operation, further refining your data analysis.
Section overview
5 resourcesIntroduces aggregation functions and grouping data for analysis.
1. Introduction
Goal
This tutorial aims to provide a comprehensive understanding of the HAVING clause in SQL. By the end of this tutorial, you'll be able to filter results of a GROUP BY operation effectively.
Learning Outcomes
You will learn:
- The purpose of the HAVING clause
- How to use the HAVING clause in conjunction with GROUP BY
- How to interpret the results
Prerequisites
Before starting this tutorial, you should have a basic understanding of SQL and how to write basic queries. Knowledge of GROUP BY clause would be beneficial.
2. Step-by-Step Guide
The HAVING clause is used in SQL to filter the results of a GROUP BY operation. Unlike the WHERE clause, which filters rows before they're grouped, the HAVING clause filters after grouping.
The general syntax is:
SELECT column_name(s)
FROM table_name
GROUP BY column_name(s)
HAVING condition;
HAVING is often used with aggregate functions (COUNT, SUM, AVG, etc.) to filter the results of GROUP BY according to a particular condition.
3. Code Examples
Example 1: Basic Usage
Consider a Orders table with columns OrderID, CustomerID, OrderDate, and Amount.
SELECT CustomerID, COUNT(OrderID)
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) > 5;
This query fetches the customers who have placed more than 5 orders. The HAVING clause is used to filter the groups created by GROUP BY clause.
Example 2: With Multiple Conditions
SELECT CustomerID, COUNT(OrderID), AVG(Amount)
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) > 5 AND AVG(Amount) > 100;
This query fetches the customers who have placed more than 5 orders and the average order amount is greater than 100.
4. Summary
The HAVING clause is a powerful tool in SQL for filtering grouped data. It's mostly used with the GROUP BY clause and aggregate functions. Remember, HAVING filters groups, while WHERE filters rows.
5. Practice Exercises
- Write a query to find products that have been ordered more than 10 times from the
OrderDetailstable. - Write a query to find customers who have spent more than 500 in total orders.
Solutions
SELECT ProductID, COUNT(OrderID)
FROM OrderDetails
GROUP BY ProductID
HAVING COUNT(OrderID) > 10;
This query groups the data by ProductID and uses the HAVING clause to filter out groups where the count of OrderID is more than 10.
SELECT CustomerID, SUM(Amount)
FROM Orders
GROUP BY CustomerID
HAVING SUM(Amount) > 500;
This query groups the data by CustomerID and uses the HAVING clause to filter out groups where the sum of Amount is more than 500.
For further practice, try to come up with your own questions using the HAVING clause. The more you practice, the more comfortable you'll become with using it.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article