Difference between revisions of "SELECT from WORLD Tutorial/ja"

From SQLZOO
Jump to navigation Jump to search
Line 109: Line 109:
 
<div>
 
<div>
  
==Two ways to be big==
+
==ビッグになる2つの道==
 
<div class='qu'>
 
<div class='qu'>
Two ways to be big: A country is '''big''' if it has an area of more than 3 million sq km or it has a population of more than 250 million.
+
ビッグになる2つの道: ビッグな国とは、面積が 3000000 平方キロ以上 または 人口が 250000000 以上の国とする。
<p class=imper>Show the countries that are big by area or big by population. Show name, population and area.</p>  
+
<p class=imper>面積か人口が大きな国を表示する。国名 人口 面積(name, population , area)を表示する。</p>  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>

Revision as of 04:57, 6 April 2018

Language:Project:Language policy [[:{{#invoke:String|sub|SELECT from WORLD Tutorial/ja
 |1
 |Expression error: Unrecognized punctuation character "{".
}}|English]]
namecontinentarea populationgdp
AfghanistanAsia6522302550010020343000000
AlbaniaEurope28748 2831741 12960000000
AlgeriaAfrica2381741 37100000 188681000000
AndorraEurope46878115 3712000000
AngolaAfrica1246700 20609294 100990000000
...
テーブル名 world

name:国名
continent:大陸
area:面積
population:人口
gdp:国内総生産


このチュートリアルでは world テーブルで SELECT コマンドを使う:

導入

このテーブルについて

全ての国の国名と大陸と人口を表示する SQL コマンドを実行して結果を観察する。

SELECT name, continent, population FROM world
SELECT name, continent, population FROM world

大きな国々

WHERE でレコードを選択する方法

人口が2億人(200000000 ゼロが8個ある)以上の国の名前を表示

SELECT name FROM world
WHERE population = 64105700
SELECT name FROM world
WHERE population>200000000

国民一人当たりの国内総生産

人口population2億人以上の国の国名nameと国民一人当たりの国内総生産を表示

国民一人当たりの国内総生産は、国内総生産 GDP を 人口 population で割った値。式は GDP/population

SELECT name, gdp/population FROM world
  WHERE population > 200000000

南アメリカを100万人単位で

大陸continentが South America の国のnamepopulationを 百万人単位 に変換して表示する。人口 population を 1000000 で割ると100万人単位の人口になる。

SELECT name, population/1000000 FROM world
  WHERE continent='South America'

フランス、ドイツ、イタリア

国名nameと人口population を France, Germany, Italy について表示する。

SELECT name, population FROM world
  WHERE name IN ('France','Germany','Italy')

ユナイテッド

国名に 'United' を含む国の国名を特定する。

SELECT name FROM world
  WHERE name LIKE '%United%'

ビッグになる2つの道

ビッグになる2つの道: ビッグな国とは、面積が 3000000 平方キロ以上 または 人口が 250000000 以上の国とする。

面積か人口が大きな国を表示する。国名 人口 面積(name, population , area)を表示する。

select name,population,area
from world
where area>3000000
or population>250000000

One or the other (but not both)

Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.

  • Australia has a big area but a small population, it should be included.
  • Indonesia has a big population but a small area, it should be included.
  • China has a big population and big area, it should be excluded.
  • United Kingdom has a small population and a small area, it should be excluded.
select name, population,area
from world
where
(population>250000000 or area>3000000)
and not(population>250000000 and area>3000000)

Rounding

Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.

For South America show population in millions and GDP in billions both to 2 decimal places.
Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.
SELECT name, ROUND(population/1000000,2),
             ROUND(gdp/1000000000,2)
  FROM world
 WHERE continent='South America'

Trillion dollar economies

Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.

Show per-capita GDP for the trillion dollar countries to the nearest $1000.

select name, ROUND(gdp/population,-3)
from world
where
gdp>1000000000000

Name and capital have the same length

Greece has capital Athens.

Each of the strings 'Greece', and 'Athens' has 6 characters.

Show the name and capital where the name and the capital have the same number of characters.

  • You can use the LENGTH function to find the number of characters in a string
SELECT name, LENGTH(name), continent, LENGTH(continent), capital, LENGTH(capital)
  FROM world
 WHERE name LIKE 'G%'
SELECT name, capital
  FROM world
 WHERE LENGTH(name)=LENGTH(capital)

Matching name and capital

The capital of Sweden is Stockholm. Both words start with the letter 'S'.

Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
  • You can use the function LEFT to isolate the first character.
  • You can use <> as the NOT EQUALS operator.
SELECT name, LEFT(name,1), capital
FROM world
SELECT name,capital
FROM world
WHERE LEFT(name,1)=LEFT(capital,1)
AND name<>capital

All the vowels

Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count because they have more than one word in the name.

Find the country that has all the vowels and no spaces in its name.

  • You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.
  • The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'
SELECT name
   FROM world
WHERE name LIKE 'B%'
  AND name NOT LIKE '%a%'
SELECT name
  FROM world
WHERE name LIKE '%a%'
AND name LIKE '%e%'
AND name LIKE '%i%'
AND name LIKE '%o%'
AND name LIKE '%u%'
AND name NOT LIKE '% %'


What Next