![acbd1d597e0dd55713280bfc1d87ef54.png](https://i-blog.csdnimg.cn/blog_migrate/a11ab36971fbd58f006fc48d03e233ac.jpeg)
第2关——简单查询
case......end表达式
更正下面链接中做错过的题
SQLZOO:SELECT from WORLD Tutorial/zhsqlzoo.net参考答案链接地址:
第2关《从零学会SQL:简单查询》练习题答案mp.weixin.qq.com![121ca419315d55808f7ff46d2bf9bec4.png](https://i-blog.csdnimg.cn/blog_migrate/7f4e160e877c6a86f9b212e803e4de0a.png)
11题
SELECT name,
( CASE
WHEN continent='Oceania' THEN 'Australasia'
ELSE continent
END )
FROM world
WHERE name LIKE 'N%';
![cf8d89216cb16ddf21b67258e93da475.png](https://i-blog.csdnimg.cn/blog_migrate/c64035f3303bb55cd18f37f230175bbb.jpeg)
12题
select name,
(case
when continent in ('Europe', 'Asia') then 'Eurasia'
when continent in ('North America', 'South America', 'Caribbean') then 'America'
else continent
end) as continent1
from world
where name like 'A%' or name like 'B%';
![e6628f546ee326f8532382d23672c7cf.png](https://i-blog.csdnimg.cn/blog_migrate/51d20e8e4e3923175c43959619a7b8d2.jpeg)
13题
select name, continent,
( case
when continent='Oceania' then 'Australasia'
when continent='Eurasia' then 'Europe/Asia'
when name='Turkey' then 'Europe/Asia'
when continent='Caribbean' and name like 'B%' then 'North America'
when continent='Caribbean' and name not like 'B%' then 'South America'
else continent
end ) as a
from world
order by name;
字符串模糊查询
更正下面链接中做错过的题
SELECT names/zh - SQLZOOsqlzoo.net参考答案链接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762555&idx=1&sn=1c57656162a01163135652fafc120e32&scene=19#wechat_redirectmp.weixin.qq.com11题
SELECT name
FROM world
WHERE name = capital;
![95d5c371686fac6aed1320629ee17536.png](https://i-blog.csdnimg.cn/blog_migrate/4651302a666e6b48dce69761a88c0024.jpeg)
12题
SELECT name
FROM world
WHERE capital = concat(name, ' City');
上面的代码中,字符串City前有空格
![653dd24dcc54057852767d89cfdb89cf.png](https://i-blog.csdnimg.cn/blog_migrate/29bbc71d8421c97c3099555732220f31.jpeg)
13题
select capital, name
from world
where capital like concat('%',name,'%');
![b8d52313b029ca68a495a72902750d2e.png](https://i-blog.csdnimg.cn/blog_migrate/3b60fc7ed9d17f224bbbcb6075e3c232.jpeg)
14题
SELECT name, capital
FROM world
WHERE capital LIKE concat('%',name,'_%');
![e728d115894c0375a17442bdf1a309b1.png](https://i-blog.csdnimg.cn/blog_migrate/1d41ac08565535b7631006cf0855481e.jpeg)
15题
select name,replace(capital, name, '')
from world
where capital Like concat(name,'%_%');
![7f0cc99a837cc16d463aa5d02c3b0020.png](https://i-blog.csdnimg.cn/blog_migrate/3877a7c8664aba663fa923a1cf753972.jpeg)
第3关——汇总分析
简单查询
更正下面链接中做错过的题
SELECT from Nobel Tutorial/zhsqlzoo.net参考答案链接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762573&idx=1&sn=3e4d24bc8c23f7d44d0473f074407e4c&scene=19#wechat_redirectmp.weixin.qq.com5题
/*
【知识点】比较运算符,逻辑运算符(and),括号里的有优先级
*/
select yr, subject, winner
from nobel
where (yr between 1980 and 1989) and subject = 'Literature';
![a6a956f0bb265402e605b3ada5f247b2.png](https://i-blog.csdnimg.cn/blog_migrate/be130917f9d1475086102ab81713570e.jpeg)
8题
/*
【知识点】比较运算符,逻辑运算符(or)
*/
select yr, subject, winner
from nobel
where ( yr = 1980 and subject = 'physics' ) or ( yr = 1984 and subject = 'chemistry' );
![f4f828cc90fca8c7fe8c6c5ddc65e16f.png](https://i-blog.csdnimg.cn/blog_migrate/70608aa846557c560797f1e00357bc13.jpeg)
12题
/*
跳脫字符:单引号
你不能把一个单引号直接的放在字符串中。但您可连续使用两个单引号在字符串中当做一个单引号。
*/
select *
from nobel
where winner = 'EUGENE O''NEILL';
![57f4fa2fb479534ac9cad0e1d26d396e.png](https://i-blog.csdnimg.cn/blog_migrate/f272281a0f910dd90310937c8cfc016d.jpeg)
13题
select winner, yr, subject
from nobel
where winner like 'Sir%'
order by yr desc, winner;
![62234845b8c44d53ff1864c902e335e2.png](https://i-blog.csdnimg.cn/blog_migrate/1014354a821032e671a72ce4413ac887.jpeg)
14题
/*
14.查找1984年获奖者和主题按主题和获胜者名称排序,
并把化学奖和物理奖排到最后面显示
之前sqlzoo网站这个是可以y运行正确的,但是现在网站可能出了问题,无法运行正确
理解这个sql就可以了
【知识点】运算符in
*/
select winner, subject
from nobel
where yr=1984
order by subject in ('Physics','Chemistry'),subject,winner;
sql解释:
subject in ('Physics','Chemistry')返回值(0或者1),
会对每一个subject做一个if的判断,有的是1,没有的是0
再用order by把这些值排序在下面
这两个科目('Physics','Chemistry')的就是0排在前边,
是这两个科目的返回1就排在后边了。
因为化学和物理科目题目要求在后面,所以引入此函数出现0、1,达成题目的要求
![ec5f75bbef9c08169086bc31993c5265.png](https://i-blog.csdnimg.cn/blog_migrate/13c65009332ff1f83ac5488f5f308e8c.jpeg)
第3关——汇总分析
汇总分析
更正下面链接中做错过的题
SUM and COUNT/zhsqlzoo.net参考答案链接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762573&idx=1&sn=3e4d24bc8c23f7d44d0473f074407e4c&scene=19#wechat_redirectmp.weixin.qq.com2题
select continent
from world
group by continent;
![8577f7124b89efb674b90846d74e00be.png](https://i-blog.csdnimg.cn/blog_migrate/6fa6c4fb55f1b0f9aeecce01a286fa63.jpeg)
7题
错误做法:
select continent, count(name)
from world
group by continent
having population > 10000000;
![d3aa6913777a58f042cb755da86e370a.png](https://i-blog.csdnimg.cn/blog_migrate/e3555d2da0dff6be4695d9c4fc6c70b4.jpeg)
错误原因:
group by产生“临时表” 后,再对临时表进行 having过滤。此题中,world这个表按continent进行group by 后,population要用聚合函数来处理,因为一个continent里多个国家,也就有多个population,没用聚合函数,它就不知道你指的是哪个population。
正确做法:
select continent,count(name)
from world
where population>=10000000
group by continent;
![85af64f975f7b468620fd1261f96ec55.png](https://i-blog.csdnimg.cn/blog_migrate/cb51938fec480dd6a064f7f9edd37a06.jpeg)
错题总结:
group by
分组之后的SQL执行语句中,必须使用聚合函数;
但有时不分组,却可以使用聚合函数,如下题:
select count( name )
from world
where area > 1000000;
![78688e3e1175b3c742edacc7c3165231.png](https://i-blog.csdnimg.cn/blog_migrate/dd4414722dcdffcd2c82f1f97591e8bd.png)
第4关——复杂查询
子查询(标量、关联)
子查询(标量、关联)用括号框住,再进行算术、比较、逻辑运算。
更正下面链接中做错过的题
SELECT within SELECT Tutorial/zhsqlzoo.net参考答案链接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762673&idx=1&sn=d30b58f8c826c145c81bc0309452f6ef&scene=19#wechat_redirectmp.weixin.qq.com3题
SELECT
NAME,continent
FROM
world
WHERE
continent IN (
( SELECT continent FROM world WHERE NAME = 'Argentina' ),
( SELECT continent FROM world WHERE NAME = 'Australia' )
)
ORDER BY NAME ASC;
# in 后面的括号中用逗号隔开单行单列的标量
另法:
select
name, continent
from world
where
continent in ( select continent from world where name='Argentina' or name='Australia'
)
order by name;
# 此解法中 in 后面的括号中的参数是多行单列的值
# 若子查询的结果是多行单列,结果集类似一个列,父查询使用IN运算符
![5743926579eab5b12e58eaa782e93bc0.png](https://i-blog.csdnimg.cn/blog_migrate/fcc2f184f66bdc4a4f5edab854cedbea.jpeg)
5题
/*
concat函数实现字符串拼接,第一个参数不要求是字符串类型,第二个参数要求是字符串类型
round函数实现第一个数值类型参数的四舍五入,第二个参数(正数)代表精确到小数点后第几位
*/
select
name, concat(
round(population /(select population from world where name='Germany')*100,0)
,'%'
)
from world
where continent = 'Europe';
![37bab6e7095ae466b8b6a7ceac235bc3.png](https://i-blog.csdnimg.cn/blog_migrate/43382a0edd8c514c43d6744e68609205.jpeg)
6题
select name
from world
where gdp > all( select gdp from world where continent = 'Europe' and gdp > 0 );
# gdp 中有空值,要增加筛选条件 gdp > 0
![708c065a97731c99aba550bbef8e16cb.png](https://i-blog.csdnimg.cn/blog_migrate/9e67dfe129c14df1d2d5e440e5a358e2.jpeg)
7题
错误做法:
SELECT continent,name ,max( area )
FROM world
where area > 0
group by continent;
![c6d2757a98e77bbd94a4fdec58926eb9.png](https://i-blog.csdnimg.cn/blog_migrate/cc63f7faefe783f73add027897561ee5.jpeg)
# 只有当某些字段单独分组时得到的结果一样时,才能联合分组
# 联合分组的好处是使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况
SELECT continent, name ,max( area )
FROM world
where area > 0
group by continent, name;
![9e068a39e780bd4ebad3a79ecf554582.png](https://i-blog.csdnimg.cn/blog_migrate/6da3836f2a80d17677ff87a954357151.jpeg)
# 分组之后,非分组列的列名全部要用聚合函数,否则报错
SELECT continent,count( name ) ,max( area )
FROM world
where area > 0
group by continent;
![2224a38b384e146fac4fd5a13cb8e09c.png](https://i-blog.csdnimg.cn/blog_migrate/759e23c20f8260bef1e18efea80b44f4.jpeg)
正确做法:
/*
【知识点】关联子查询
一般来说 先执行子查询,但关联子查询例外。有关联子查询时,先执行主查询再执行关联子查询。
*/
SELECT
continent,
NAME,
area
FROM
world AS x
WHERE
area = ( SELECT max( area ) FROM world AS y WHERE x.continent = y.continent );
# 两张临时表x,y通过字段continent进行关联
![7d617bdcb1c3e30b82e001169cd980c4.png](https://i-blog.csdnimg.cn/blog_migrate/8ca0d2788741f463a0e754267ab81ec0.jpeg)
8题
SELECT
continent,
NAME
FROM
world AS x
WHERE
NAME <= ALL ( SELECT NAME FROM world AS y WHERE y.continent = x.continent );
![bde60c308809a759ade50f0646620848.png](https://i-blog.csdnimg.cn/blog_migrate/e427074e6044d24dd8e6e621cf778f03.jpeg)
9题
SELECT NAME,
continent,
population
FROM
world AS x
WHERE
25000000 >= ALL ( SELECT population FROM world AS y WHERE y.continent = x.continent );
![29bfaf38bd77b6bf49f3a1ca43d84aa0.png](https://i-blog.csdnimg.cn/blog_migrate/61475474fec51c4f2a1ae6ea19658733.jpeg)
10题
错误做法:
SELECT NAME
,
continent
FROM
world AS x
WHERE
population >= 3 * ALL ( SELECT population FROM world AS y WHERE x.continent = y.continent );
![c7c8b3a43cc5d477087fcff3cbb56db3.png](https://i-blog.csdnimg.cn/blog_migrate/aed93bc02837a86b64b1782f62bebebf.jpeg)
正确做法:
SELECT NAME,
continent
FROM
world AS x
WHERE
population > ALL ( SELECT 3 * population FROM world AS y WHERE y.continent = x.continent AND x.NAME <> y.NAME );
![438f4d24d4f996856459215d9e540dde.png](https://i-blog.csdnimg.cn/blog_migrate/fb8632c4dbf92671eb413ba41cb22385.jpeg)
第5关——多表查询
多表查询
更正下面链接中做错过的题
The JOIN operation/zhsqlzoo.net参考答案链接地址:
第5关《从零学会SQL:多表查询》练习题答案mp.weixin.qq.com![3f1bf87b14453c75966dbb4f07db841f.png](https://i-blog.csdnimg.cn/blog_migrate/e316447c56a5c38e594de6b486a6091b.png)
8题
/*
问题分析:
1)查找进球球员姓名,对战双方
2)条件:有一个球队是德国(球队编号'GER'),被射入了球
*/
SELECT DISTINCT b.player
FROM
game AS a INNER JOIN goal AS b ON a.id = b.matchid
WHERE
( b.teamid = a.team1 AND a.team2 = 'GER' ) OR ( b.teamid = a.team2 AND a.team1 = 'GER');
/*
1)解释下where子句中的条件:
主队是德国或者客队是德国,比如
德国 和 A对比赛(德国是主队),进球的是A队
A队和德国比赛(德国是客队),进球的是A队
所以条件是(b.teamid = a.team1 and a.team2 = 'GER') or (b.teamid = a.team2 and a.team1 = 'GER')
2)内联结的结果中入门球员有重复值,用distinct去掉重复值
*/
![6f8e499e4217c0a34ede90d3ee56929a.png](https://i-blog.csdnimg.cn/blog_migrate/bb0870cb204b0265a459883684a444de.jpeg)
9题
select c.teamname, count(c.teamname)
from eteam as c inner join goal as b on c.id=b.teamid
group by c.teamname;
![8d49471ae9186dd90d1ac79b4def9dbb.png](https://i-blog.csdnimg.cn/blog_migrate/ae704482734380cfe5ad58a195816333.jpeg)
group by
分组之后的SQL执行语句中,非分组列的列名全部要用聚合函数,否则报错;但有时不分组,却可以使用聚合函数
11题
错误解法:
select a.id,a.mdate, count(b.player)
from game as a inner join goal as b on a.id = b.matchid
where (team1 = 'POL' or team2 = 'POL')
group by a.id;
![47c2bc93b0835b1007893e90efb9a936.png](https://i-blog.csdnimg.cn/blog_migrate/c8c6505c854cd7ed4332669e655ee52d.jpeg)
错误原因:
group by
分组之后的SQL执行语句中,非分组列的列名全部要用聚合函数,否则报错;但有时不分组,却可以使用聚合函数
正确解法一:
利用联合分组,使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况
# 只有当某些字段单独分组时得到的结果一样时,才能联合分组
# 联合分组的好处是使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况
select a.id,a.mdate, count(b.player)
from game as a inner join goal as b on a.id = b.matchid
where (team1 = 'POL' or team2 = 'POL')
group by a.id, a.mdate;
![d0b8141e2f117c4c02b62287e1cacd83.png](https://i-blog.csdnimg.cn/blog_migrate/bb7cf99570056d3a9fe0b38b454317fa.jpeg)
正确解法二:
利用子查询与多表连接,将需要用的字段从少到多一一串起来
SELECT x.`賽事編號`, y.mdate, x.`入球數字`
FROM
( SELECT
a.id as `賽事編號`,
count( b.player ) as `入球數字`
FROM
game AS a
INNER JOIN goal AS b ON a.id = b.matchid
WHERE
( team1 = 'POL' OR team2 = 'POL' )
GROUP BY
a.id ) as x INNER JOIN game AS y ON x.`賽事編號` = y.id;
![6351344c32d6f9da60bc26986bc9a608.png](https://i-blog.csdnimg.cn/blog_migrate/fdecd4000bf09bcd284ea616fe94f868.jpeg)
12题
# 只有当某些字段单独分组时得到的结果一样时,才能联合分组
# 联合分组的好处是使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况
select a.id,a.mdate,count(player)
from game as a inner join goal as b on a.id=b.matchid
where b.teamid='GER'
group by a.id,a.mdate;
![effb6c854bddab000df33de852003e66.png](https://i-blog.csdnimg.cn/blog_migrate/a14c74f2ff05af48edc48453cdc11d57.jpeg)
13题
# 只有当某些字段单独分组时得到的结果一样时,才能联合分组
# 联合分组的好处是使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况
SELECT
a.mdate,
a.team1,
sum( CASE WHEN a.team1 = b.teamid THEN 1 ELSE 0 END ) AS score1,
a.team2,
sum( CASE WHEN a.team2 = b.teamid THEN 1 ELSE 0 END ) score2
FROM
game AS a
LEFT JOIN goal AS b ON a.id = b.matchid
GROUP BY
a.id,
a.mdate,
a.team1,
a.team2
ORDER BY
a.mdate,
a.id,
a.team1,
a.team2;
![ff8421eafb1642583ec8591530127f3e.png](https://i-blog.csdnimg.cn/blog_migrate/28ff636889db6db53da116cc0bf14528.jpeg)