sql zoo 刷题 SELECT from WORLD Tutorial

中文题目

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

顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。

SELECT name,round(gdp/population,-3) FROM world
WHERE (gdp/1000000000000)> 1

这道题考round的用法-3表示整数位左移3位取0 即保留千位 1000

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.

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

这道题考case when 以及其嵌套用法

英文题目

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.

  • You can use the function LEFT to isolate the first character.
  • You can use <> as the NOT EQUALS operator.
SELECT name, capital
FROM world 
where LEFT(name,1) = LEFT(capital,1) and name <> capital

这道题考LEFT 函数,函数用于从字符串的左侧提取指定数量的字符。平时比较少用,可以注意一下。

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.

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 '%a%'
  AND name LIKE '%e%'
  AND name LIKE '%i%'
  AND name LIKE '%o%'
  AND name LIKE '%u%'
  AND name NOT LIKE '% %'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容