1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

Guide to GROUP BY and HAVING clauses in MySQL with examples

  • ScientificTools.org
  • November 23, 2023 at 9:30 AM
  • 1,160 times read
Contents [hideshow]
  1. Understanding GROUP BY Clause
    1. Basic Syntax
  2. 1. Simple Grouping
    1. Example: Counting Records
  3. 2. Grouping by Multiple Columns
    1. Example: Grouping by More Than One Column
  4. Understanding HAVING Clause
    1. Basic Syntax
  5. 3. Filtering Groups with HAVING
    1. Example: Filtering Groups
    2. Example: HAVING with Multiple Conditions
  6. 4. Combining WHERE, GROUP BY, and HAVING
    1. Example: Using WHERE with GROUP BY and HAVING

In MySQL, aggregation and filtering of grouped data is often done using the GROUP BY and HAVING clauses. This guide will help you understand these concepts with practical examples.

Understanding GROUP BY Clause

The GROUP BY clause groups rows with the same values in specified columns into summary rows. It's often used with aggregate functions like COUNT(), MAX(), MIN(), SUM(), and AVG().

Basic Syntax

SQL
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name;

1. Simple Grouping

Example: Counting Records

To count the number of orders each customer has placed:

SQL
SELECT CustomerID, COUNT(OrderID) AS NumberOfOrders
FROM Orders
GROUP BY CustomerID;

This query groups the orders by CustomerID and counts the number of orders for each customer.

2. Grouping by Multiple Columns

Example: Grouping by More Than One Column

To find the total sales for each product in each store:

SQL
SELECT StoreID, ProductID, SUM(Sales) AS TotalSales
FROM Sales
GROUP BY StoreID, ProductID;

This groups the sales data first by StoreID, then by ProductID.

Understanding HAVING Clause

The HAVING clause is used to filter groups created by GROUP BY based on a specified condition. It's similar to WHERE, but HAVING works on aggregated data.

Basic Syntax

SQL
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
HAVING group_condition;

3. Filtering Groups with HAVING

Example: Filtering Groups

To find customers who have placed more than 5 orders:

SQL
SELECT CustomerID, COUNT(OrderID) AS NumberOfOrders
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) > 5;

This query groups the orders by CustomerID and includes only those groups where the count of orders is greater than 5.

Example: HAVING with Multiple Conditions

To find products with an average rating above 4 and more than 10 reviews:

SQL
SELECT ProductID, AVG(Rating) AS AverageRating, COUNT(ReviewID) AS NumberOfReviews
FROM Reviews
GROUP BY ProductID
HAVING AVG(Rating) > 4 AND COUNT(ReviewID) > 10;

4. Combining WHERE, GROUP BY, and HAVING

Example: Using WHERE with GROUP BY and HAVING

To find stores with sales in 2021 where the total sales exceeded $100,000:

SQL
SELECT StoreID, SUM(Sales) AS TotalSales
FROM Sales
WHERE Year = 2021
GROUP BY StoreID
HAVING SUM(Sales) > 100000;

This query filters records from 2021 (WHERE), groups them by StoreID, and then applies the HAVING condition on the grouped data.

  • Previous Article MySQL ORDER BY Clause guide with practical examples
  • Next Article Joining Tables in MySQL guide with examples

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™