自己做的主要是中文版,但是目前发现中英文的题目并不完全相同…有部分是摘自英文版

world表:

name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000

name: 国家名称
continent:
area: 面积
population: 人口
gdp: 国内生产总值

SELECT from WORLD Tutorial/zh

1
2
1.觀察運行一個簡單的SQL命令的結果。
SELECT name, continent, population FROM world
1
2
3
2.顯示具有至少2億人口的國家名稱。 2億是200000000,有八個零。
SELECT name FROM world
WHERE population>200000000
1
2
3
4
3.找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。
select name,gdp/population
from world
where population>200000000
1
2
3
4
4.顯示'South America'南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000)得可得到以百萬為單位人口數。
select name,population/1000000
from world
where continent = 'South America'
1
2
3
4
5.顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。
select name,population
from world
where name in('France','Germany','Italy')
1
2
3
6.顯示包含單詞“United”為名稱的國家。
select name from world
where name LIKE '%United%'
1
2
3
4
5
7.成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。
展示大國的名稱,人口和面積。
select name,population,area
from world
where area > 3000000 or population > 250000000
1
2
3
4
5
8.美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。
顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。
select name,population,area
from world
where (area > 3000000 and population <= 250000000) or (area <= 3000000 and population > 250000000)
1
2
3
4
5
9.除以為10000006個零)是以百萬計。除以10000000009個零)是以十億計。使用 ROUND 函數來顯示的數值到小數點後兩位。
對於南美顯示以百萬計人口,以十億計2位小數GDP。
select name,round(population/1000000,2),round(gdp/1000000000,2)
from world
where continent = 'South America'
1
2
3
4
5
10.顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000
顯示萬億元國家的人均國內生產總值,四捨五入到最近的$1000
select name,round(gdp/population,-3)
from world
where gdp >= 1000000000000

More difficult

1
2
3
4
5
6
7
8
11.The CASE statement shown is used to substitute North America for Caribbean in the third column.
Show the name - but substitute Australasia for Oceania - for countries beginning with N.(我明明选了zh捏...)

SELECT name,
CASE WHEN continent='Oceania' THEN 'Australasia'
ELSE continent END
FROM world
WHERE name LIKE 'N%'
1
2
3
4
5
6
7
8
9
10
11
12.Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B

select name,
case when continent in ('Europe','Asia')
then 'Eurasia'
when continent in ('North America','South America','Caribbean')
then 'America'
else continent
end
from world
where name LIKE 'A%' or name LIKE 'B%'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
13.Put the continents right...
Oceania becomes Australasia
Countries in Eurasia and Turkey go to Europe/Asia
Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America
Show the name, the original continent and the new continent of all countries.(这个题的运行口貌似有点问题一直显示错误,在12题处运行就没有问题,但是不能有空格)

select name,continent,
case when continent = 'Oceania'
then 'Australasia'
when continent in ('Eurasia','Turkey')
then 'Europe/Asia'
when continent = 'Caribbean' and name like 'B%'
then 'North America'
when continent = 'Caribbean'
then 'South America'
else continent
end
from world

SELECT from WORLD Tutorial

1
2
3
4
5
6
7
11.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.

SELECT name,capital
FROM world
WHERE length(name) = length(capital)
1
2
3
4
5
6
12.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.

select name,capital
from world
where left(name,1)=left(capital,1) and name !=capital
1
2
3
4
5
6
13.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.

select name
from world
where name not like '% %' and name like '%a%' and name like '%e%' and name like '%i%' and name like '%o%' and name like '%u%'

Quiz

GTZGTWH_I5U___AYPNEOC66.png
1.選擇代碼找出以U開頭的國家名稱。
Answer:

1
2
3
SELECT name
FROM world
WHERE name LIKE 'U%'

2.選擇代碼以顯示英國United Kingdom的人口。
Answer:

1
2
3
SELECT population
FROM world
WHERE name = 'United Kingdom'

3.這個SQL代碼有什麼錯處。預計結果應該是含’France’的單列。

1
2
3
SELECT continent 
FROM world
WHERE 'name' = 'France'

Answer:

1
'name' 應該是 name

4.選擇你會從這個代碼獲得的結果。

1
2
3
SELECT name, population / 10 
FROM world
WHERE population < 10000

Answer:
4.png
5.選擇代碼以顯示在歐洲和亞洲的國家的名稱和人口。
Answer:

1
2
3
SELECT name, population
FROM world
WHERE continent IN ('Europe', 'Asia')

6.選擇代碼,結果只有兩行。
Answer:

1
2
SELECT name FROM world
WHERE name IN ('Cuba', 'Togo')

7.選擇你會從這個代碼獲得的結果。

1
2
3
SELECT name FROM world
WHERE continent = 'South America'
AND population > 40000000

Answer:
7.png

Summary

  • round(num,p) 函数:
    num保留p位小数,例如round(7253.86, 0) -> 7254,返回数值
  • length(s) 函数:
    返回字符串s的长度
  • left(s,p) 函数:
    从字符串s开头截取p个长度的子串,left(‘Hello’,4) -> ‘Hell’
  • case when 函数:
    相当于if函数,但是case when更加适用于多条件的判断,返回值就在语句then和else后,case可以在不同条件下返回不同的值;主要用于条件聚合,打标签进行分类聚合的场景