MYSQL
June 3, 2026 1 min read

Mastering MYSQL Group By and Having: Efficient Data Grouping and Filtering CAPSCOM TECHNOLOGY Computer Education Written By

HS
HEMANT SINGH
Technical Writer & Educator

GROUP BY

GROUP BY, group the records from a table.

Table: users

idusernamecity
1AmanDelhi
2RakeshMumbai
3KamalMumbai
4BharatDelhi
5OmPunjab

 

Count users which have the same city

SELECT COUNT(id) AS total_users, city FROM users GROUP BY city;
total_userscity
2Delhi
2Mumbai
1Punjab

 

HAVING

Display all those records where users are more than 1 and belong to the same city.

SELECT COUNT(id) AS total_users, city FROM users GROUP BY city HAVING COUNT(id)>1;
total_userscity
2Delhi
2Mumbai