Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

GROUP BY and HAVING in MySQL | Aggregation in MySQL

Ujjwal Abhishek
Ujjwal Abhishek

GROUP BY

The GROUP BY clause is used to arrange the rows in a group using a particular column value. If there are multiple rows with the same value for a column then all those rows will be grouped with that column value. It is used with the SELECT statement and is placed after the WHERE clause and before the ORDER BY if used any.

Syntax:

SELECT column1,column2,…
FROM table_name
WHERE condition
GROUP BY column1,column2, …
Order BY column1, column2, ….

The GROUP BY clause is generally used with aggregate functions like SUM, AVG, COUNT, MAX, MIN. The aggregate functions provide information about each group.

1. GROUP BY clause without aggregate functions

cricketers  
nameagecountry
Virat33India
Rohit34India
ABD37South Africa
Williamson31New Zealand
Stokes30England
Boult32New Zealand
Finch35Australia
Warner35Australia

For example:

SELECT country
FROM cricketers
GROUP BY country; 

Output:

country
India
South Africa
New Zealand
England
Australia

In the above example, the country is selected and grouped according to the country which ensures unique occurrences of each country.

2. GROUP BY clause with aggregate functions

For example:

SELECT COUNT(name) AS number_of_players, country
FROM cricketers
GROUP BY country;

Output:

number_of_playerscountry
2India
1South Africa
2New Zealand
1England
2Australia

Similarly, when GROUP BY is used with multiple columns, all those rows will be grouped which have the same value for all the columns used for grouping.

HAVING

The HAVING clause is used with the GROUP BY clause in a query to specify come conditions and filter some groups or aggregates that fulfill those conditions. The difference between WHERE and HAVING is, WHERE is used to filter rows, and HAVING is used to filter groups by applying some conditions.

Syntax:

SELECT column1, column2, …
FROM table_name
WHERE conditions
GROUP BY column1, column2, …..
HAVING conditions

For example:

cricketers  
nameagecountry
Virat33India
Rohit34India
ABD37South Africa
Williamson31New Zealand
Stokes30England
Boult32New Zealand
Finch35Australia
Warner35Australia
SELECT COUNT(name) AS number_of_players, country
FROM cricketers
GROUP BY country
HAVING number_of_players>1;

Output:

number_of_playerscountry
2India
2New Zealand
2Australia

In the above example, the players are grouped according to their country and those countries which have the number of players >1 are selected using the HAVING clause.

Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet