Programming

SQL COUNT: how many rows

COUNT counts rows. GROUP BY splits them by city.

All lessons · SQL lessons: read, sort, count

COUNT: count

COUNT(*) counts the rows in the table. Together with WHERE it counts only the rows that match. The result is one number, not a list of names.

SELECT COUNT(*)
FROM users
WHERE city = 'Baku';

GROUP BY: group

GROUP BY gathers rows of the same city into one group. COUNT says how many people are in each group. Baku is one row, Ganja is another.

SELECT city, COUNT(*)
FROM users
GROUP BY city;

Questions

Does COUNT show the names? No. It shows only the number.

What is the star in COUNT(*)? It means count every row. It does not skip empty columns.

Can you write the city and COUNT without GROUP BY? Most databases reject that. The column you show must be in the group.

Does this page change a database? No. It is a sample query. If you have no table of your own, there is no result.

Author: Nibras Code