GROUP BY
GROUP BY, group the records from a table.
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 |