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 '% %'
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容