SQL练习(1)

SQL练习-SQLZOO

SQLZOO:SELECT from WORLD Tutorial

 

表结构如下所示: 

 

name:国家名称

continent:州

area:面积

population:人口

gdp:生产总值

  1. 展示具有至少2亿人口的国家名称。 
    SELECT name FROM world WHERE population>200000000
  2. 找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。
    SELECT name,gdp/population FROM world WHERE population>200000000
  3. 顯示'South America'南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000)得可得到以百萬為單位人口數。
    SELECT name,population/1000000 FROM world WHERE continent = 'South America'
  4. 顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。
    SELECT name,population FROM world WHERE name in ('France','Germany', 'Italy')
  5. 顯示包含單詞“United”為名稱的國家。
    SELECT name FROM world WHERE name like '%United%'
  6. 成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。展示大國的名稱,人口和面積。

    SELECT name, population,area FROM world WHERE area>3000000 or population>250000000
  7. 美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積

    SELECT name, population,area FROM world WHERE (area>3000000 and population<250000000) or (population>250000000 and area<3000000)
  8. 除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND 函數來顯示的數值到小數點後兩位。對於南美顯示以百萬計人口,以十億計2位小數GDP。

    SELECT name, round(population/1000000,2),round(gdp/1000000000,2) FROM world WHERE continent= 'South America'
    

    round函数用于数据的四舍五入。round(x,d),其中x表示要处理的数,d表示保留几位小数,值得注意的是,d可以为负数,表示小数点左边d位正数为0

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

    SELECT name, round(gdp/population,-3) FROM world WHERE gdp>1000000000000 
  10. 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. 所示的CASE语句用于在第三栏中用北美代替加勒比海地区,显示大陆的名字-但用澳大利亚替换大洋洲 -对国家开始N.

    SELECT name,
           CASE WHEN continent='Oceania' THEN 'Australasia'
                ELSE continent END
      FROM world
     WHERE name LIKE 'N%'
  11. 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%'
  12. 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.

    将各大洲摆放正确...

    • 大洋洲成为澳大利亚洲
    • 欧亚大陆和土耳其的国家前往欧洲/亚洲
    • 以'B'开头的加勒比海岛屿前往北美,其他加勒比海岛屿前往南美
    显示所有国家的名称,原始大陆和新大陆。
    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
posted @ 2020-05-15 13:26  sansan114  阅读(340)  评论(0)    收藏  举报