SELECT .. GROUP BY

From SQLZOO
Revision as of 15:46, 16 July 2012 by Connor (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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:

games
yrcitycontinent
2000SydneyAustralia
2004AthensEurope
2008BeijingAsia
2012LondonEurope

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

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense