SQL題集解答-2

鏈接直達

1.Read the notes about this table. Observe the result of running a simple SQL command.

SELECT name,
       continent,
       population
FROM world

2.How to use WHERE to filter records. Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.

SELECT name
FROM world
WHERE population>200000000

3.Give the name and the per capita GDP for those countries with a population of at least 200 million.

SELECT name,
       GDP/population
FROM world
WHERE population>200000000

4.Show the name and population in millions for the countries of the continent 'South America'. Divide the population by 1000000 to get population in millions.

SELECT name,
       population/1000000
FROM world
WHERE continent='South America'

5.Show the name and population for France, Germany, Italy


SELECT name,
       population
FROM world
WHERE name IN ('France',
               'Germany',
               'Italy')

6.Show the countries which have a name that includes the word 'United'


SELECT name
FROM world
WHERE name LIKE '%United%'

7.Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.
Show the countries that are big by area or big by population. Show name, population and area.


SELECT name,
       population,
       area
FROM world
WHERE area>3000000
  OR population>250000000

8.USA, India, and China are big in population and big by area. Exclude these countries.
Show the countries that are big by area or big by population but not both. Show name, population and area.

SELECT name,
       population,
       area
FROM world
WHERE (area>3000000
       AND population<250000000)
  OR (area<3000000
      AND population>250000000)

9.Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.
For South America show population in millions and GDP in billions to 2 decimal places.

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

10.Show the per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
Show per-capita GDP for the trillion dollar countries to the nearest $1000.

SELECT name,
       ROUND(GDP/population/1000,0)*1000
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.

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

SELECT name,
       CASE continent
           WHEN 'Europe' THEN 'Eurasia'
           WHEN 'Asia' THEN 'Eurasia'
           WHEN ('North America'
                 OR 'South America') THEN 'America'
           WHEN '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
  • Order by country name in ascending order
    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 name='Turkey' THEN 'Europe/Asia'
           WHEN continent='Caribbean'
                AND name LIKE 'B%' THEN 'North America'
           WHEN continent='Caribbean' THEN 'South America'
           ELSE continent
       END AS 'New Continent'
FROM world
ORDER BY name ASC
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末葫哗,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌纺酸,老刑警劉巖抹缕,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件随抠,死亡現(xiàn)場離奇詭異红伦,居然都是意外死亡尊流,警方通過查閱死者的電腦和手機虐杯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門玛歌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人擎椰,你說我怎么就攤上這事支子。” “怎么了达舒?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵值朋,是天一觀的道長。 經(jīng)常有香客問我巩搏,道長昨登,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任塔猾,我火速辦了婚禮篙骡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘丈甸。我一直安慰自己糯俗,他們只是感情好,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布睦擂。 她就那樣靜靜地躺著得湘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪顿仇。 梳的紋絲不亂的頭發(fā)上淘正,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天摆马,我揣著相機與錄音,去河邊找鬼鸿吆。 笑死囤采,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的惩淳。 我是一名探鬼主播蕉毯,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼思犁!你這毒婦竟也來了代虾?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤激蹲,失蹤者是張志新(化名)和其女友劉穎棉磨,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體学辱,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡乘瓤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了策泣。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片馅扣。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖着降,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情拗军,我是刑警寧澤任洞,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站发侵,受9級特大地震影響交掏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜刃鳄,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一盅弛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧叔锐,春花似錦挪鹏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至步责,卻和暖如春返顺,著一層夾襖步出監(jiān)牢的瞬間禀苦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工遂鹊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留振乏,地道東北人。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓秉扑,卻偏偏與公主長得像慧邮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子邻储,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內(nèi)容