Filtering Grouped Data with HAVING
This content piece explores the concept of combining filtering with grouping in SQL, showcasing how to effectively filter aggregate functions using 'having' clauses.
Lets Go!

Filtering Grouped Data with HAVING
Lesson 21
Learn how to use the HAVING clause to filter results after grouping, typically used with aggregate functions.
Get Started 🍁Introduction to SQL Filtering and Grouping
Welcome to our course on SQL filtering and grouping! In this course, we will explore how to combine filtering with grouping in SQL to manipulate and retrieve data efficiently.
Have you ever wondered how to filter based on the result of an aggregate function? Or how to use different keywords like having
and order by
to filter and sort data effectively? If so, this course is perfect for you!
We will cover essential concepts such as the order of execution, the use of aggregate functions, and how to translate business questions into the correct SQL filters. By the end of this course, you will have a solid understanding of how to filter individual and grouped records in SQL.
Are you ready to dive into the world of SQL filtering and grouping? Let's get started!
Main Concepts of SQL Filtering and Grouping
-
Combining Filtering with Grouping: The video emphasizes that in SQL, aggregate functions cannot be filtered using the
WHERE
clause. Instead, the keywordHAVING
is used for filtering based on the results of aggregate functions. This ensures that filtering occurs after the grouping and aggregation. -
HAVING Keyword: Groups have their own special filtering word
HAVING
. It filters grouped records based on specified conditions. For example, the query in the video shows only years in which more than 10 films were released using theHAVING
keyword. -
Order of Execution: The order of execution in SQL queries is crucial. The sequence is
SELECT
,FROM
,WHERE
,GROUP BY
,HAVING
,ORDER BY
, andLIMIT
. Understanding this order helps in writing effective queries and knowing when to use specific keywords likeHAVING
. -
Differentiation between WHERE and HAVING: The video explains that
WHERE
filters individual records, whileHAVING
filters grouped records. This distinction is important when writing queries that involve aggregations and filtering based on these aggregations. -
Translating Business Questions into Queries: By breaking down business questions into smaller steps, it becomes easier to translate them into SQL queries. The example questions presented in the video demonstrate how to approach querying for specific information, whether it requires simple filtering or complex aggregation and grouping.
Practical Applications of SQL Filtering and Grouping
In this section, we will walk through practical applications of filtering and grouping in SQL based on the concepts discussed in the video transcript.
Step-by-Step Guide:
-
Filtering Based on Aggregate Functions:
- When filtering based on aggregate functions, use the
HAVING
keyword instead of theWHERE
clause. - For example, if you want to show only the years in which more than 10 films were released:
SELECT release_year FROM films GROUP BY release_year HAVING COUNT(title) > 10;
- When filtering based on aggregate functions, use the
-
Translating Business Questions into Queries:
-
Question 1: What films were released in the year 2000?
- Translate into SQL query:
SELECT title FROM films WHERE release_year = 2000;
- Translate into SQL query:
-
Question 2: In what years was the average film duration over 2 hours?
- Break down the query into smaller steps:
SELECT release_year FROM films GROUP BY release_year HAVING AVG(duration) > 120;
- Break down the query into smaller steps:
-
Try It Out:
-
Filtering Based on Aggregate Functions:
- Run the provided SQL query to filter years with more than 10 films released.
- Observe how the
HAVING
keyword is used to filter grouped records.
-
Translating Business Questions into Queries:
- Create and execute SQL queries for both business questions discussed.
- Notice how grouping and aggregation are essential for answering these questions accurately.
By following these steps, you can practice applying filtering and grouping concepts in SQL to solve real-world scenarios effectively. Enjoy exploring SQL queries and enhancing your data analysis skills!
Test your Knowledge
What is the purpose of the HAVING clause?
What is the purpose of the HAVING clause?
Advanced Insights into SQL Filtering and Grouping
In SQL, combining filtering with grouping can provide more nuanced insights into your data. When filtering based on the result of an aggregate function, we cannot use the WHERE clause. Instead, we utilize a special filtering word for groups called HAVING
. This allows us to filter based on grouped records rather than individual ones, making it a powerful tool for analytical queries.
Tips and Recommendations:
- Remember,
WHERE
filters individual records, whileHAVING
filters grouped records. - When using
HAVING
, keep in mind the order of execution in SQL queries: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT.
Expert Advice:
Utilize aliases with HAVING
to create more readable queries and consider the order of operations to ensure accurate results in your SQL analysis.
Curiosity Question:
How might you approach a complex business question using a combination of filtering, grouping, and aggregation in SQL to derive key insights from your data?
Deepening your understanding of SQL filtering and grouping can unlock new possibilities for data analysis and interpretation. Experiment with different combinations of filters, groups, and aggregate functions to gain valuable insights into your dataset.
Additional Resources for SQL Grouping and Filtering
- Article: Mastering the GROUP BY and HAVING clauses in SQL
- Video Tutorial: Understanding SQL GROUP BY and HAVING Clauses
- Book: "SQL Queries for Mere Mortals" by John L. Viescas
- Interactive Practice: SQL GROUP BY and HAVING exercises
Dive deeper into SQL grouping and filtering concepts with these additional resources. Explore different examples, practice exercises, and expert insights to enhance your understanding and proficiency in SQL queries. Happy learning! 📚✨
Practice
Task: Write an SQL query to:
Task: Show customers who made more than 5 orders.
Task: List categories where the total product quantity exceeds 100