36、SQLZOO 练习答案 SELECT from WORLD Tutorial

https://sqlzoo.net/
SQLZOO:SELECT from WORLD Tutorial
在这里插入图片描述
name:國家名稱
continent:洲份
area:面積
population:人口
gdp:國內生產總值

Country Profile
在這教程中,我們會使用SELECT語句,對World表格進行查詢。

1、閱讀此表的注意事項 觀察運行一個簡單的SQL命令的結果。

select name, continent, population from world

2.如何使用WHERE來篩選記錄。 顯示具有至少2億人口的國家名稱。 2億是200000000,有八個零。

select name from world where population>=200000000


3.找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。

select name ,gdp/population from world where population>=200000000


4.顯示’South America’南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000)得可得到以百萬為單位人口數。

select name,population/1000000 from world where continent=‘South America’


5.顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。

select name,population from world where name in (‘France’, ‘Germany’, ‘Italy’)


6.顯示包含單詞“United”為名稱的國家。

select name from world where name like’%United%’


7.成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。
展示大國的名稱,人口和面積。


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


8.美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。
顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。


select name ,population, area from world where (area>=3000000 and population <=250000000) or (area <3000000 and population >250000000)


9.除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND 函數顯示數值到小數點後兩位。對於南美顯示以百萬計人口,以十億計2位小數GDP。

select name,round(population/1000000,2),round(gdp/1000000000,2) from world where continent =‘south America’


10.顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000。
顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。


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


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.
在第三栏列中,所示的情况说明用于替代北美而不是加勒比海。显示名称-但用澳大拉西亚代替大洋洲-以N开头的国家。

select name,case
when continent=‘Oceania’ then ‘Australasia’
else continent end
from world
where name like ‘N%’



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
显示名称和大陆-但是用欧亚大陆代替欧洲和亚洲;用美洲代替北美、南美或加勒比的每个国家。显示以A或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%’



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
以“B”开头的加勒比海群岛前往北美,其他加勒比海群岛前往南美
Show the name, the original continent and the new continent of all countries.
显示所有国家的名称、原始大陆和新大陆。

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


在这个练习中,主要接触两个新的函数

1、ROUND函数:四舍五入函数。返回数字表达式同时四舍五入为指定的长度、精度。
ROUND(指定数值,保留小数点位数),保留为正数时,表示四舍五入到所制定的小数位数;为负数时,表示四舍五入到指定小数点左边的位数。

示例:
ROUND(838.234,1) 返回为:838.2
ROUND(835.578,2) 返回为:835.58
ROUND(867.43,-1) 返回为:870
ROUND(933.643,-2) 返回为:900
ROUND(945.8,-3) 返回为:1000

2、简单Case函数和Case搜索函数。
case when then else end :通过对值进行判断后,回写指定值,常用于分类、分组、分析
——简单Case函数
select CASE sex
WHEN ‘1’ THEN ‘男’
WHEN ‘2’ THEN ‘女’
ELSE ‘其他’ END
from table_A

——Case搜索函数
select CASE WHEN sex = ‘1’ THEN ‘男’
WHEN sex = ‘2’ THEN ‘女’
ELSE ‘其他’ END
from table_B

发布了38 篇原创文章 · 获赞 5 · 访问量 6013

猜你喜欢

转载自blog.csdn.net/luluisntlulu/article/details/100581509