Difference between revisions of "SELECT .. GROUP BY"
From SQLZOO
| (7 intermediate revisions by 5 users not shown) | |||
| Line 9: | Line 9: | ||
<tr><td>2004</td><td align='left'>Athens</td><td align='left'>Europe</td></tr> | <tr><td>2004</td><td align='left'>Athens</td><td align='left'>Europe</td></tr> | ||
<tr><td>2008</td><td align='left'>Beijing</td><td align='left'>Asia</td></tr> | <tr><td>2008</td><td align='left'>Beijing</td><td align='left'>Asia</td></tr> | ||
| − | <tr><td> | + | <tr><td>2019</td><td align='left'>London</td><td align='left'>Europe</td></tr> |
| + | <tr><td>2016</td><td align='left'>india</td><td align='left'>asia</td></tr> | ||
| + | <tr><td>2018</td><td align='left'>india</td><td align='left'>asia</td></tr> | ||
| + | <tr><td>2014</td><td align='left'>england</td><td align='left'>Europe</td></tr> | ||
</table> | </table> | ||
<div class='ht'> | <div class='ht'> | ||
| − | <div class=params>schema:scott</div> | + | <div class='params'>schema:scott</div> |
<source lang=sql class='tidy'> DROP TABLE games</source> | <source lang=sql class='tidy'> DROP TABLE games</source> | ||
<source lang=sql class='setup'> CREATE TABLE games( | <source lang=sql class='setup'> CREATE TABLE games( | ||
Revision as of 13:06, 16 March 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:
| yr | city | continent |
|---|---|---|
| 2000 | Sydney | Australasia |
| 2004 | Athens | Europe |
| 2008 | Beijing | Asia |
| 2019 | London | Europe |
| 2016 | india | asia |
| 2018 | india | asia |
| 2014 | england | Europe |
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