Which query counts users per country?
Options
- SELECT country, COUNT(*) FROM users GROUP BY country; (Correct option)
- SELECT COUNT(country) FROM users GROUP BY *;
- SELECT country FROM users COUNT BY country;
- SELECT * FROM users GROUP BY COUNT(country);
Answer
SELECT country, COUNT(*) FROM users GROUP BY country;
Why
GROUP BY country puts rows with the same country together into groups.
COUNT(*) then counts the rows inside each group.
The result has one row per country, e.g. ('Peru', 120), ('Spain', 85).
Remember
GROUP BY + COUNT = counts per category.