SQL Zoo 練習題答案—SELECT within SELECT & SUM and COUNT

SELECT within SELECT

1.List each country name where the population is larger than that of 'Russia'.

SELECT name FROM world WHERE population > (SELECT population FROM world WHERE name='Russia')

2.Show the countries in Europe with a per capita GDP greater than 'United Kingdom'.

SELECT name FROM world WHERE gdp/population > (SELECT gdp/population FROM world WHERE name = 'United Kingdom') AND continent = 'Europe'

3.List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.

SECLECT name,continent FROM world WHERE continent IN (SELECT continent FROM world WHERE name IN('Argentina','Australia')) ORDER BY name

4.Which country has a population that is more than Canada but less than Poland? Show the name and the population.

SELECT name, population FROM world WHERE population > (SELECT population FROM world WHERE name = 'Canada') AND population < (SELECT population FROM world WHERE name = 'Poland')

這一題用 BETWEEN AND 不行荣茫,因為BETWEEN AND 是包含首尾的咏雌。

5.Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE name = 'Germany')*100,0),'%') FROM world WHERE continent = 'Europe'

6.Which countries have a GDP greater than every country in Europe? [Give the nameonly.] (Some countries may have NULL gdp values)

select name from world where gdp > ALL(select gdp from world where gdp > 0 and continent='Europe')

ALL/SOME/ANY 的用法章贞,學習URL

7.Find the largest country (by area) in each continent, show the continent, the nameand the area:

SELECT continent, name, area FROM world x WHERE area >= ALL (SELECT area FROM world y WHERE y.continent=x.continent AND area>0)

8.List each continent and the name of the country that comes first alphabetically.

SUM and COUNT

1.Show the total population of the world.

SELECT SUM(population)FROM world

2.List all the continents - just once each.

SELECT DISTINCT(continent) FROM world

3.Give the total GDP of Africa

SELECT SUM(gdp) FROM world WHERE continent = 'Africa'

4.How many countries have an area of at least 1000000

SELECT COUNT(name) FROM world WHERE area >= 1000000

5.What is the total population of ('Estonia', 'Latvia', 'Lithuania')

SELECT SUM(population) FROM world WHERE name IN ('Estonia', 'Latvia', 'Lithuania')

6.For each continent show the continent and number of countries.

SELECT continent,COUNT(name) FROM world GROUP BY continent

7.For each continent show the continent and number of countries with populations of at least 10 million.

SELECT continent,COUNT(name) FROM world WHERE population >= 10000000 GROUP BY continent

8.List the continents that have a total population of at least 100 million.

SELECT continent FROM world GROUP BY continent HAVING SUM(population) >= 100000000

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市括饶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖瓢阴,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件况木,死亡現(xiàn)場離奇詭異垒拢,居然都是意外死亡,警方通過查閱死者的電腦和手機火惊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門求类,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人屹耐,你說我怎么就攤上這事尸疆。” “怎么了惶岭?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵寿弱,是天一觀的道長。 經(jīng)常有香客問我按灶,道長症革,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任鸯旁,我火速辦了婚禮噪矛,結(jié)果婚禮上量蕊,老公的妹妹穿的比我還像新娘。我一直安慰自己艇挨,他們只是感情好残炮,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缩滨,像睡著了一般势就。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上楷怒,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天蛋勺,我揣著相機與錄音,去河邊找鬼鸠删。 笑死抱完,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的刃泡。 我是一名探鬼主播巧娱,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼烘贴!你這毒婦竟也來了禁添?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤桨踪,失蹤者是張志新(化名)和其女友劉穎老翘,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锻离,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡铺峭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了汽纠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卫键。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖虱朵,靈堂內(nèi)的尸體忽然破棺而出莉炉,到底是詐尸還是另有隱情,我是刑警寧澤碴犬,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布絮宁,位于F島的核電站,受9級特大地震影響翅敌,放射性物質(zhì)發(fā)生泄漏羞福。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一蚯涮、第九天 我趴在偏房一處隱蔽的房頂上張望治专。 院中可真熱鬧卖陵,春花似錦、人聲如沸张峰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽喘批。三九已至撩荣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間饶深,已是汗流浹背餐曹。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留敌厘,地道東北人台猴。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像俱两,于是被迫代替她去往敵國和親饱狂。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,505評論 0 23
  • 《打造最強大腦》第五章 記憶也要講策略 在記憶法的學習中宪彩,除了能夠應(yīng)用記憶術(shù)把知識記住休讳,還要有策略:通過對記...
    沈陽一莊曉娟閱讀 465評論 3 9
  • 文|烏木君 夢里游長河,流水聲如歌尿孔。 眺望孔明燈俊柔,微光照茅舍。 我將言語思盡活合,無一言能和婆咸。 你月光般的眼神勝一畫秋...
    阿特洛斯閱讀 303評論 0 0
  • 無意做了個測試游戲,左腦35歲芜辕,右腦8歲。理性思維占據(jù)主導块差,感性思維讓我單純侵续,敏銳而充滿靈性,這個測試正是我喜歡的...
    卿瑩閱讀 139評論 0 0