डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

Full Stack Web Development with Laravel

Full Stack Web Development with Laravel

Affiliate Program

Affiliate Program

MYSQL GROUP BY HAVING clause

GROUP BY

GROUP BY, group the records from a table.

Table: users
id username city
1 Aman Delhi
2 Rakesh Mumbai
3 Kamal Mumbai
4 Bharat Delhi
5 Om Punjab

 

Count users which have the same city

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

 

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_users city
2 Delhi
2 Mumbai

 

© 2016 - 2023, All Rights are Reserved.