1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLZOO 「SUM and COUNT」 回答集

Last updated at Posted at 2018-09-08

###<2020/08/15 追記>
現在、SQLの学習をSQLZOOで開始するのはあまりお勧めできません。
別のサービスの活用をご検討ください。


SQLZOOの「SUM and COUNT」の回答集です。


1 . Total world population

SELECT SUM(population)
FROM world

そのまま

2 . List of continents

SELECT DISTINCT continent 
FROM world

DISTINCTでユニーク化

3 . GDP of Africa

SELECT SUM(gdp) 
FROM world
WHERE continent = 'Africa'

4 . Count the big countries

SELECT COUNT(name) 
FROM world
WHERE area >= 1000000

5 . Baltic states population

SELECT SUM(population)
FROM world
WHERE name IN ('Estonia', 'Latvia', 'Lithuania')

6 . Counting the countries of each continent

SELECT continent, COUNT(name)
FROM world
GROUP BY continent

continentごとに集計する、という指定のために「GROUP BY」が必要です。

7 . Counting big countries in each continent

SELECT continent, COUNT(name)
FROM world
WHERE population >= 10000000
GROUP BY continent

WHERE句とGROUP BY句の順番に注意しましょう。

8 .

SELECT continent
FROM world
GROUP BY continent
 HAVING SUM(population) >= 100000000

HAVING句は集計した後の結果で結果が絞られます。
WHERE句は集計する前の値で結果を絞るので、この場合は不適当。
書く順序に違和感があるかも知れませんが、GROUP BY句のあとにHAVING句が読み込まれます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?