MysqlZoo--quiz of SELECT in SELECT

本文详细介绍了如何使用SQL查询来比较不同国家的人口、GDP,如哪些国家人口超过加拿大且低于波兰,以及欧洲国家的GDP最高记录。还展示了如何按洲份找出最大面积的国家和每个洲的第一大国。
摘要由CSDN通过智能技术生成

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

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

(括号中select出的值本身就可以当作一个具体的数,相当于返回Russia的人数)

二、列出歐州每國家的人均GDP,當中人均GDP要高於英國'United Kingdom'的數值。


select name from world 
where gdp/population>
(select gdp/population from world 
where name='United Kingdom')
 and continent='Europe'

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

select name,continent from world 
where continent in 
(select continent from world 
where name in ('Argentina','Australia')) 
order by name

在阿根廷Argentina 及 澳大利亞 Australia所在的洲份中,用 in 代表多选一,符合其中一个即可。在SQL中,排序默认的是升序(asc),降序要加上desc(order by name desc)。

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

select name,population from world 
where
population>(select population from world where name='Canada') 
and 
population<(select population from world where name='Poland')

瞪眼法秒了。

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

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

select name,
concat(round(round(population/
(select population from world 
where name = 'germany'),6)*100,0),'%')
from world
where continent='europe'

concat:连接作用,eg:concat(1,2)-->12

round:保留小数位数,eg:round(12.334,1)-->12.3;round(12.334,0)-->12;:round(12.334,-1)-->10

整套代码分为以下部分:

1、第一部分population/(select population from worldwhere name = 'germany'),这是用来算两者        相除所得的数,设这部分为X

2、第二部分round(X,6),这是保留6位小数(当然保留10000位都行,你开心就行),设这部分为Y

3、第三部分round(Y*100,0),*100是为了化为百分数,0代表保留整数,设这部分为Z

4、最后一部分concat(Z,'%'),即将Z与%连接在一起,比如Z=11,结果为11%

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

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

(有些國家的記錄中,GDP是NULL,沒有填入資料的。)

上面两个代码都能用,所以他的提示有啥用呢

all表示将单个值与子查询返回的单列值集进行比较

七、在每一個州中找出最大面積的國家,列出洲份 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)

这个自己领悟吧

相当于创建了两个表下x,y,WHERE y.continent=x.continent,意为查询y表中和x表一样的,括号中的内容是y表中area的数值

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

select continent,name from world a 
where a.name=(select name from world b where 
              a.continent=b.continent 
              order by name limit 1)    

每洲只有列一國,就是只返回第一行数据呗,用limit 1

大体与第七题相同

九、找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字namecontinent 洲份和population人口。

select name,continent,population from world a
where 25000000>=all(
                    select population from world b where
                    a.continent=b.continent and population>0
                   )

这跟上面一题有啥差别呢?

十、有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent

select name,continent from world a
where population/3>=all(
                      select population from world b where
                      a.continent=b.continent and population>0
                      and b.name<>a.name
                     )

这个其实在all中用cout函数也行,但上面的代码更简洁,注意要加上 b.name<>a.name,以免把自身算进去(<>是不等于的意思)

  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值