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.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces 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

  1. Write a query to find products that have been ordered more than 10 times from the OrderDetails table.
  2. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help