码迷,mamicode.com
首页 > 数据库 > 详细

SQL练习-SQLZOO SQLZOO:SELECT within SELECT Tutorial

时间:2020-05-24 12:02:27      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:body   man   tin   bsp   sele   最大   with   tab   nbsp   

此教程教我們在SELECT查詢中使用別一個SELECT查詢,進行一些更複雜的查詢。

name國家名continent洲份area面積population人口gdp國民生產總值
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
...

1.列出每個國家的名字 name,當中人口 population 是高於俄羅斯‘Russia‘的人口。

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


2.列出歐州每國家的人均GDP,當中人均GDP要高於英國‘United Kingdom‘的數值。

select name from world
where gdp/population > (select gdp/population from world where name =‘united kingdom‘) and continent =‘Europe‘


3.在阿根廷Argentina 及 澳大利亞 Australia所在的洲份中,列出當中的國家名字 name 及洲分 continent 。按國字名字順序排序

select name,continent from world
where continent in(‘South America‘,‘Oceania‘)
order by name asc

4.哪一個國家的人口比加拿大Canada的多,但比波蘭Poland的少?列出國家名字name和人口population 。

select name,population from world
where population<(select population from world where name=‘poland‘) and population >(select population from world where name=‘canada‘)

5.Germany德國(人口8000萬),在Europe歐洲國家的人口最多。Austria奧地利(人口850萬)擁有德國總人口的11%。

顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。

(暂时不会)


6.哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)

select name from world
where gdp >all(select gdp from world where continent=‘europe‘and gdp>0)

7.在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)

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

8.列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)

(暂时不会)

SQL练习-SQLZOO SQLZOO:SELECT within SELECT Tutorial

标签:body   man   tin   bsp   sele   最大   with   tab   nbsp   

原文地址:https://www.cnblogs.com/cat30/p/12946958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!