SELECT .. GROUP BY
From SQLZOO
SELECT .. GROUP BY
Host cities and continents for the Olympics Games are stored in the table games.
Notice that Europe appears in the table twice:
| yr | city | continent |
|---|---|---|
| 2000 | Sydney | Australia |
| 2004 | Athens | Europe |
| 2008 | Beijing | Asia |
| 2012 | London | Europe |
In a GROUP BY statement only distinct values are shown for the column in the GROUP BY. This example shows the continents hosting the Olympics with the count of the number of games held.
schema:scott
DROP TABLE games
CREATE TABLE games(
yr INTEGER PRIMARY KEY, city VARCHAR(10), continent VARCHAR(10);
INSERT INTO games VALUES (
SELECT continent, COUNT(yr) FROM games GROUP BY continent
See also