MySQL

Group By and Having Clause


The GROUP BY and HAVING clauses in SQL are used together to group rows based on common values in specified columns and then filter those groups. While the GROUP BY clause groups the data, the HAVING clause applies a condition to the grouped results, similar to how the WHERE clause works for individual rows.

 

 

Key Differences Between WHERE and HAVING:

  • WHERE: Filters rows before the grouping operation.
  • HAVING: Filters groups after the aggregation (used with aggregate functions like COUNT(), SUM(), etc.).

 

Syntax:

SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1
HAVING aggregate_function(column2) condition;
 

 

Example: GROUP BY and HAVING Clause

Let’s say you have a Sales table:

SaleIDProductIDSaleDateQuantityTotalAmount
11012023-01-105500
21012023-02-15101000
31022023-01-203450
41032023-03-108800
51012023-04-052200

 

Example 1: Grouping by Product and Counting Sales

SELECT ProductID, COUNT(SaleID) AS SaleCount
FROM Sales
GROUP BY ProductID;
 

Result:

ProductIDSaleCount
1013
1021
1031

 

This groups the sales by ProductID and counts how many sales each product had. Now, let’s say you only want to return products that have more than one sale. This is where the HAVING clause comes in.

 

 

Example 2: Using HAVING to Filter Groups

SELECT ProductID, COUNT(SaleID) AS SaleCount
FROM Sales
GROUP BY ProductID
HAVING COUNT(SaleID) > 1;

 

Result:

ProductIDSaleCount
1013

 

This query groups the sales by ProductID and counts the number of sales per product. The HAVING clause filters out products that have only 1 sale, showing only products with more than 1 sale.

 

 

Common Use Cases for GROUP BY and HAVING

 

Filtering Groups Based on Aggregates:

  • Finding departments with more than a certain number of employees.
  • Retrieving products that have sold more than a certain amount.

Summarizing Data:

  • Grouping sales by region and filtering out regions that don’t meet a sales threshold.

Advanced Reporting:

  • Getting only the groups that satisfy certain conditions after aggregation (like total sales, average sales, etc.).