Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Filtering Aggregated Results | Aggregations for KPIs
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL for Business Intelligence Analysts

bookFiltering Aggregated Results

When working with aggregated data in SQL, you often need to filter groups based on the results of aggregate functions, such as SUM, COUNT, or AVG. The HAVING clause is designed specifically for this purpose, allowing you to refine your results after the aggregation step. This is essential for identifying key performance indicators (KPIs) like top customers or best-selling products, which depend on aggregated metrics rather than simple row-level conditions.

1234
SELECT product_id, SUM(total_amount) FROM sales GROUP BY product_id HAVING SUM(total_amount) > 100;
copy

In this example, you are grouping sales by product_id and calculating the total sales amount for each product. The HAVING clause filters out any product whose total sales amount does not exceed 1000. This is different from the WHERE clause, which filters rows before aggregation takes place. If you want to filter based on the result of an aggregate function, you must use HAVING. If you need to filter rows before aggregation, such as restricting to a certain date range or product category, you use WHERE.

1234
SELECT customer_id, COUNT(*) FROM sales GROUP BY customer_id HAVING COUNT(*) > 5;
copy

This query finds customers who have made more than five purchases. By grouping sales by customer_id and using COUNT(*), you aggregate the number of sales per customer. The HAVING clause then filters out any group (customer) with five or fewer sales, leaving only your top repeat customers. This technique is invaluable for identifying your most engaged or valuable customers, as well as your best-selling products. When you use HAVING with aggregate functions, you can focus your analysis on the highest performers, set thresholds for KPIs, or spotlight outliers in your data.

Note
Definition

The HAVING clause is used in SQL to filter groups created by GROUP BY based on the result of aggregate functions, such as SUM, COUNT, or AVG. It applies after the aggregation step, unlike WHERE, which filters rows before aggregation.

You can combine WHERE and HAVING in a single query for even more precise analysis. Use WHERE to limit the rows before aggregation, such as selecting sales from a certain year or region, and then use HAVING to filter the aggregated results. This approach ensures your calculations are both accurate and relevant to your business questions.

1. When should you use HAVING instead of WHERE?

2. What does HAVING COUNT(*) > 5 filter for?

3. How can HAVING help identify high-performing products?

question mark

When should you use HAVING instead of WHERE?

Select the correct answer

question mark

What does HAVING COUNT(*) > 5 filter for?

Select the correct answer

question mark

How can HAVING help identify high-performing products?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the difference between WHERE and HAVING with an example?

How can I use multiple aggregate functions with HAVING?

What happens if I use HAVING without GROUP BY?

bookFiltering Aggregated Results

Swipe to show menu

When working with aggregated data in SQL, you often need to filter groups based on the results of aggregate functions, such as SUM, COUNT, or AVG. The HAVING clause is designed specifically for this purpose, allowing you to refine your results after the aggregation step. This is essential for identifying key performance indicators (KPIs) like top customers or best-selling products, which depend on aggregated metrics rather than simple row-level conditions.

1234
SELECT product_id, SUM(total_amount) FROM sales GROUP BY product_id HAVING SUM(total_amount) > 100;
copy

In this example, you are grouping sales by product_id and calculating the total sales amount for each product. The HAVING clause filters out any product whose total sales amount does not exceed 1000. This is different from the WHERE clause, which filters rows before aggregation takes place. If you want to filter based on the result of an aggregate function, you must use HAVING. If you need to filter rows before aggregation, such as restricting to a certain date range or product category, you use WHERE.

1234
SELECT customer_id, COUNT(*) FROM sales GROUP BY customer_id HAVING COUNT(*) > 5;
copy

This query finds customers who have made more than five purchases. By grouping sales by customer_id and using COUNT(*), you aggregate the number of sales per customer. The HAVING clause then filters out any group (customer) with five or fewer sales, leaving only your top repeat customers. This technique is invaluable for identifying your most engaged or valuable customers, as well as your best-selling products. When you use HAVING with aggregate functions, you can focus your analysis on the highest performers, set thresholds for KPIs, or spotlight outliers in your data.

Note
Definition

The HAVING clause is used in SQL to filter groups created by GROUP BY based on the result of aggregate functions, such as SUM, COUNT, or AVG. It applies after the aggregation step, unlike WHERE, which filters rows before aggregation.

You can combine WHERE and HAVING in a single query for even more precise analysis. Use WHERE to limit the rows before aggregation, such as selecting sales from a certain year or region, and then use HAVING to filter the aggregated results. This approach ensures your calculations are both accurate and relevant to your business questions.

1. When should you use HAVING instead of WHERE?

2. What does HAVING COUNT(*) > 5 filter for?

3. How can HAVING help identify high-performing products?

question mark

When should you use HAVING instead of WHERE?

Select the correct answer

question mark

What does HAVING COUNT(*) > 5 filter for?

Select the correct answer

question mark

How can HAVING help identify high-performing products?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
some-alt