SQLZOO练习题(1)
生活随笔
收集整理的這篇文章主要介紹了
SQLZOO练习题(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SQLZOO練習題(1)
sqlzoo上較難的一些sql查詢練習題及其解題思路。
題目:
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, casewhen 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'endelse continentend from world order by name asc思路:
主要涉及case函數的用法。
當滿足condition1時,返回value1,滿足condition2,返回value2,都不滿足返回def_value。
該題解法在于使用一個case函數的嵌套。讓以B開頭的加勒比島國屬于北美,非B開頭的屬于南美。在父case when continent = ‘caribbean’ then后應該填入需要返回的value。但是這里我們需要做一個判斷,所以再嵌套一個case,判斷when name like 'B%'時返回北美,否則返回南美。這樣經過判斷后的北美和南美就作為子case返回的value傳遞給父case,填入到then后面的返回值中。
希望本文對你有所幫助。
總結
以上是生活随笔為你收集整理的SQLZOO练习题(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DDR(三)DDR工作时序与原理
- 下一篇: 4 SELECT within SELE