Difference between revisions of "SELECT .. GROUP BY"

From SQLZOO
Jump to: navigation, search
Line 1: Line 1:
  
 
<h1>SELECT .. GROUP BY</h1>
 
<h1>SELECT .. GROUP BY</h1>
Host cities and continents for the Olympics Games are stored in the table <code>scott.games</code>.
+
Host cities and continents for the Olympics Games are stored in the table <code>games</code>.
 
Notice that Europe appears in the table twice:  
 
Notice that Europe appears in the table twice:  
 
<table>
 
<table>

Revision as of 19:00, 22 January 2013

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
2000SydneyAustralasia
2004AthensEurope
2008BeijingAsia
2012LondonEurope
schema:scott
 DROP TABLE games
 CREATE TABLE games(
  yr INTEGER,
  city VARCHAR(20),
  continent VARCHAR(20));
INSERT INTO games VALUES (2000,'Sydney','Australasia');
INSERT INTO games VALUES (2004,'Athens','Europe');
INSERT INTO games VALUES (2008,'Beijing','Asia');
INSERT INTO games VALUES (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.

SELECT continent, COUNT(yr) FROM scott.games
 GROUP BY continent
SELECT continent, COUNT(yr) FROM games
 GROUP BY continent

See also

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense