SQL ZOO练习记录(SELECT within SELECT Tutorial/zh)

本文记录了作者在SQL ZOO网站上的练习经历,主要涉及使用SELECT子句解决各种数据查询问题,包括比较国家人口、GDP、面积等方面。内容涵盖了如何找出人口高于特定国家、人均GDP超过特定数值、国家人口比例计算等多个实际问题的解题思路和技巧,同时提到了查询中遇到的数据表问题和函数应用,如concat()和round()。
摘要由CSDN通过智能技术生成

sql zoo练习入口

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

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

这题给出了代码,改了一下国家名就完事啦。

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

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

这题对我来说有两个坑:

  1. 人均GDP=gdp/population(QAQ显得我好没文化啊,这都不知道的样子)
  2. 列出的是name,最开始我选择列出的是人均GDP,就错了。(害,这是我的锅嘛。。好吧,是的)

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

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

这个数据表有点问题,那个名字都是按字母排列的,所以不用最后那句order by也可以通过。

第四题:哪一個國家的人口比加拿大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。以德國的人口的百分比作人口顯示。

第一种想法是直接用8000万人,但显然是不对滴。

select name, concat(round((population/80000000)*100,2),'%') as population
from world
where continent ='Europe';

第二种是用子查询的方法:

select name, concat(round(population/(
	select population from world 
	where name = 'Germany')*100,2),'%') as population
from world
where continent ='Europe';

这两种结果好像是一样的吧?没显示对错,先这样吧。
这题复习了concat()和round()函数。

all词的用法小启发:
用法:>=、<=、>、<后再加all
例子中提到:查詢找到世界上最大的國家(以人口計算):

SELECT name
  FROM world
 WHERE population >= ALL(SELECT population
                           FROM world
                          WHERE population>0);
 #子查询中的大于0,是为了排除null值。

这里也可以用来统计最高/最低工资,不需要使用orderby后再用limit函数来取数了。(去重的问题要另外考虑)

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

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

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

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

SELECT continent, name
FROM world x
WHERE name <= ALL
(SELECT name
FROM world y
WHERE x.continent = y.continent);

这个地方x.continent = y.continent其实类似group by的作用,但是感觉没有group by那么大限制。

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

SELECT name,continent,population 
FROM world AS x
WHERE 25000000 >= ALL(SELECT population FROM world y 
					WHERE x.continent = y.continent)
AND population >0;

这里同理,用x.continent = y.continent进行分组!!

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

select name, continent
from world x
where population/3 > all( select population from world y 
						where x.continent = y.continent 
						and y.name != x.name)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值