sql線上練習網站(http://sqlzoo.cn)答案解析(1)

iSQlServer發表於2009-10-14

一:SELECThttp://www.sqlzoo.cn/1.htm
    1a. 檢視關於bbc表的詳細說明
        SELECT name, region, population FROM bbc
    2a. 給出人口不小於2億的國家的名稱.(2億是200000000,有8個0)
        SELECT name FROM bbc WHERE population>= 200000000
    2b. 給出人口不小於2億的國家的名稱和人均 GDP
        SELECT name, gdp/population FROM bbc WHERE population >=200000000
    2c. 給出地區屬於'South Asia'的國家的名稱和人口數(以百萬為單位,人口數除以1000000,然後用round函式取整)
        SELECT name, round(population/1000000,0) FROM bbc WHERE region = 'South Asia'
    2d. 給出'France', 'Germany', 'Italy'三個國家的名稱和人口
        SELECT name, population FROM bbc WHERE name in ('France', 'Germany', 'Italy')
    2e. 給出包含'United'字元國家名稱
        SELECT name FROM bbc WHERE name LIKE '%United%'

二:SELECT within SELECThttp://www.sqlzoo.cn/1a.htm
    1a. 給出人口多於Russia(俄國)的國家名稱
        SELECT name FROM bbc  WHERE population > (SELECT population FROM bbc WHERE name='Russia')
    1b. 給出'India'(印度), 'Iran'(伊朗)所在地區的所有國家的所有資訊
        SELECT * FROM bbc WHERE region IN (SELECT region FROM bbc WHERE name IN ('India','Iran'))
    1c. 給出人均GDP超過'United Kingdom'(英國)的歐洲國家
        SELECT name FROM bbc WHERE region = 'Europe' AND gdp/population > (SELECT gdp/population FROM bbc WHERE name = 'United Kingdom')
    1d. 哪個國家的人口比Canada(加拿大)多但少於Algeria(阿爾及利亞)?
        SELECT name FROM bbc WHERE population > (SELECT population FROM bbc WHERE name = 'Canada') AND population < (SELECT population FROM bbc WHERE name = 'Algeria')
    2a. 給出GDP比任何歐洲國家都多的國家(只要顯示國家名稱)
        SELECT name FROM bbc WHERE gdp > ALL(SELECT gdp FROM bbc WHERE region = 'Europe')
    3a. 給出每個地區人口最大的國家
        SELECT name FROM bbc x  WHERE population >= ANY (SELECT population FROM bbc y WHERE x.region = y.region)
    or:  SELECT name FROM bbc x WHERE population = (SELECT max(population) FROM bbc y WHERE x.region = y.region)
    or:  SELECT name FROM bbc WHERE population IN (SELECT max(population) FROM bbc GROUP BY region)
    3b. 給出地區中所有國家的人口總數為0的地區
        SELECT region FROM bbc WHERE 0 = ALL(SELECT sum(population) FROM bbc GROUP BY region)
    or:  SELECT region FROM bbc x WHERE 0 = (SELECT sum(population) FROM bbc y WHERE x.region = y.region)
    or:  SELECT region FROM bbc GROUP BY region HAVING sum(population) = 0
    3c. 有些國家的人口數比她的周邊國家(周邊國家指在同一地區的國家)要多三倍,列出這些國家和地區
        SELECT name,region FROM bbc x WHERE population > all(SELECT population*3 FROM bbc y WHERE x.region=y.region AND x.name!=y.name)

三:More about SELECThttp://www.sqlzoo.cn/1b.htm
    1a. 修改下面的查詢,顯示1950諾貝爾獎得主
        SELECT yr, subject, winner  FROM nobel WHERE yr = 1950
    1b. 給出1962 文學獎獲得者
        SELECT winner FROM nobel WHERE yr = 1962 AND subject = 'Literature'
    2a. 顯示'Albert Einstein'獲獎的年份和獎項
        SELECT yr,subject FROM nobel WHERE winner = 'Albert Einstein'
    2b. 給出2000年以來(包含2000年)和平獎的得主
        SELECT winner FROM nobel WHERE yr >= 2000 AND subject ='Peace'
    2c. 給出1980 到 1989間文學獎獲得者的所有資訊
        SELECT * FROM nobel WHERE subject='Literature' AND yr BETWEEN 1980 AND 1989
    2d. 給出獲獎者為下列總統的所有資訊. ('Theodore Roosevelt', 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter')
        SELECT * FROM nobel WHERE winner IN ('Theodore Roosevelt', 'Woodrow Wilson', 'Jed Bartlet', 'Jimmy Carter')
    2e. 名字的第一個字元為John的獲獎者名稱
        SELECT winner FROM nobel WHERE winner LIKE 'John%'

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-616526/,如需轉載,請註明出處,否則將追究法律責任。

相關文章