---Advertisement---

SQL GROUP BY and HAVING Clause: A Complete Guide with Examples Great 2025

By Manisha

Updated On:

---Advertisement---
SQL GROUP BY and HAVING Clause: A Complete Guide with Examples Great 2025

The GROUP BY clause in SQL is used to group rows with the same values, often alongside aggregate functions like COUNT, SUM, AVG, etc. The HAVING clause helps filter grouped data based on specific conditions. In this guide, we will explore their syntax, usage, and real-world examples.


  1. SQL GROUP BY and HAVING Clause: What is the SQL GROUP BY Clause?
  2. SQL GROUP BY Syntax
  3. Grouping by a Single Column
  4. Grouping by Multiple Columns
  5. Using GROUP BY with Aggregate Functions
  6. Filtering Grouped Data with the HAVING Clause
  7. Real-World Examples
  8. Summary

SQL GROUP BY and HAVING Clause: The SQL GROUP BY clause is used to group rows with the same values into a single row. It is typically used with aggregate functions to generate summary reports from the database.

Key Features of GROUP BY:

โœ… Groups rows that share the same values in specified columns.
โœ… Often used with aggregate functions (COUNT, SUM, AVG, MAX, MIN).
โœ… Returns one row per group instead of individual rows.


SQL GROUP BY and HAVING Clause: Hereโ€™s the basic syntax for using GROUP BY in SQL:

sql

SELECT column_name, aggregate_function(column_name)

FROM table_name

WHERE condition

GROUP BY column_name

HAVING condition;

Explanation:

  • SELECT column_name, aggregate_function(column_name): Selects the columns to display along with an aggregate function (optional).
  • FROM table_name: Specifies the table from which data is fetched.
  • WHERE condition: (Optional) Filters data before grouping.
  • GROUP BY column_name: Groups the results based on the specified column(s).
  • HAVING condition: (Optional) Filters grouped data after aggregation.

SQL GROUP BY and HAVING Clause: To understand the GROUP BY clause, letโ€™s take an example using a members table.

Example: List Unique Genders in a Members Table

sql

SELECT gender FROM members GROUP BY gender;

gender
Female
Male

This groups the results by gender, showing only unique values.


SQL GROUP BY and HAVING Clause: You can group by more than one column to get more specific groupings.

Example: Group Movies by Category and Release Year

sql

SELECT category_id, year_released 

FROM movies 

GROUP BY category_id, year_released;

category_idyear_released
12011
22008
62007
82005

โœ… Here, movies are grouped by category and release year, reducing duplicate entries.


SQL GROUP BY and HAVING Clause: Aggregate functions help summarize grouped data, making the GROUP BY clause more useful.

Example: Count the Number of Members by Gender

sql

SELECT gender, COUNT(membership_number) 

FROM members 

GROUP BY gender;

genderCOUNT(membership_number)
Female3
Male5

โœ… The COUNT function counts how many male and female members exist in the database.


SQL GROUP BY and HAVING Clause: The HAVING clause is used after GROUP BY to filter grouped results based on a condition.

Example: Find Movie Categories with More Than One Release Year

sql

SELECT category_id, COUNT(year_released) 

FROM movies 

GROUP BY category_id 

HAVING COUNT(year_released) > 1;

category_idCOUNT(year_released)
83

โœ… The HAVING clause filters results to only show categories where movies were released in more than one year.


Example 1: Get Total Sales Per Product

sql

SELECT product_name, SUM(quantity_sold) 

FROM sales 

GROUP BY product_name;

โ†’ This shows the total sales per product.

Example 2: Find Customers Who Have Placed More Than 5 Orders

sql

SELECT customer_id, COUNT(order_id) 

FROM orders 

GROUP BY customer_id 

HAVING COUNT(order_id) > 5;

โ†’ Only customers with more than 5 orders are displayed.

Example 3: Find Average Salary Per Department

sql

SELECT department, AVG(salary) 

FROM employees 

GROUP BY department;

โ†’ This calculates the average salary for each department.


โœ… SQL GROUP BY is used to group rows with the same values.
โœ… It is commonly used with aggregate functions like COUNT, SUM, AVG, MIN, and MAX.
โœ… GROUP BY can be used with multiple columns for more detailed analysis.
โœ… The HAVING clause filters grouped results based on aggregate function conditions.
โœ… It is widely used in business reports, data analysis, and summary statistics.

By mastering SQL GROUP BY and HAVING, you can efficiently analyze large datasets and generate meaningful reports!

MYSQL Order BY

Download My SQL

Leave a Comment

Index