主知识点一:select&from 自己完整敲一遍知识点中出现的代码吧~ 主知识点二:where 【1】SELECT from WORLD Tutorial - SQLZOO第四题 【题目】 查询南美洲(south america)所有国家的名称以及它们以百万(1000000 )为单位的人口数量
【正确答案】 select name , population/1000000 population_in_millions from world where continent = 'South America'; 【2】SELECT from Nobel Tutorial - SQLZOO第九题 【题目】 查询1980年除诺贝尔化学奖和诺贝尔医学奖外其余奖项获奖者的所有信息。
【正确答案】 select * from nobel where yr = 1980 and subject not in ('Chemistry','Medicine') 【3】SELECT from WORLD Tutorial - SQLZOO第十三题 【题目】 查询既包含有所有元音字母(a,e,i,o,u),同时国家名中没有空格的国家,最后显示他们的名字 例如,赤道几内亚Equatorial Guinea 和 多米尼加共和国Dominican Republic ,都包括有五个元音字母(a,e,i,o,u),但这些国家不会被记录,因为国家名中,由两个单词构成(有空格)
【正确答案】 select name from world where name like '%a%' and name like '%e%' and name like '%i%' and name like '%o%' and name like '%u%' and name not like '% %'; 【4】SELECT from Nobel Tutorial - SQLZOO第十题 【题目】 查询1910年以前(不含1910)诺贝尔医学奖获得者和2004年及以后诺贝尔文学奖获得者的所有信息
【正确答案】 select * from nobel where (subject = 'Medicine' and yr < 1910) or (subject = 'Literature' and yr >= 2004) 主知识点三:聚合函数、group by&having 【1】SUM and COUNT - SQLZOO第五题 【题目】 计算Estonia, Latvia, Lithuania这几个国家的总人口数
【正确答案】 select sum(population) from world where name in ('Estonia', 'Latvia', 'Lithuania') 【2】SUM and COUNT - SQLZOO第七题 【题目】 查询每个大洲和该大洲里人口数超过1千万的国家的数量
【正确答案】 select continent ,count(name) 'number of countries' from world where population >= 10000000 group by continent 主知识点四:order by 【1】点击链接SELECT from Nobel Tutorial - SQLZOO第十四题 【题目】 查询1984年所有获奖者的姓名和奖项。结果将诺贝尔化学奖和物理学奖排在最后,然后按照奖项排序,再按照获奖者姓名排序
【正确答案】 select winner ,subject from nobel where yr = 1984 order by subject in ('chemistry','physics') , subject, winner 主知识点五:limit 主知识点六:子查询 【1】SELECT within SELECT Tutorial - SQLZOO第一题 【题目】 查询 人口(population)比俄罗斯Russia更多的国家名(name)
【正确答案】 select name from world where population > ( select population from world where name = 'Russia' ) 【2】SELECT within SELECT Tutorial - SQLZOO第七题 【题目】 查找每个大陆(continent)中最大的国家(按区域area),显示该大陆(continent),名称(name)和区域(area)
【正确答案】 select continent ,name ,area from world where (continent,area) in ( select continent ,max(area) from world group by continent ) 【3】SELECT within SELECT Tutorial - SQLZOO第二题 【题目】 查询在欧洲Europe中人均gdp大于 英国 'United Kingdom' 的国家名
【正确答案】 select name from world where continent = 'Europe' and gdp / population > ( select gdp / population from world where name = 'United Kingdom' ) 【4】SELECT within SELECT Tutorial - SQLZOO第四题 【题目】 查询国家的人口数(population)超过加拿大(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' )
主知识点七:表连接 【1】More JOIN operations - SQLZOO第十一题 【题目】 查询演员Rock Hudson每年出演超过2部电影的年份和这些年他出演电影的数量。
【参考答案】 select m.yr ,count(m.title) from movie m join casting c on m.id=c.movieid join actor a on a.id=c.actorid where a.name = 'Rock Hudson' group by m.yr having count(m.title)>2 【2】More JOIN operations - SQLZOO第十二题 【题目】 查询Julie Andrews出演过的所有电影的电影名和该电影的主演
【参考答案】 select m.title ,a.name from movie m join casting c on m.id = c.movieid join actor a on a.id = c.actorid where c.ord = 1 and c.movieid in ( select c.movieid from actor a join casting c on a.id = c.actorid join movie m on c.movieid = m.id where a.name = 'Julie Andrews' ) 【3】More JOIN operations - SQLZOO第十三题 【题目】 查询至少出演过第1主角15次的演员名 和例题讲解的题目一样,尝试不使用子查询完成
【参考答案】 select name from casting c join actor a on c.actorid = a.id where ord = 1 group by name having count(movieid)>=15 主知识点八:常用函数 【1】SELECT from WORLD Tutorial - SQLZOO第十二题 【题目】 查询国家名称及其首都名称都以相同的字母开头的国家名称及其首都,且不能包括国家名称和首都名称完全相同的情况
【参考答案】 select name ,capital from world where left(name, 1) = left(capital, 1) and name != capital; 【2】SELECT names - SQLZOO第十四题 【题目】 查询首都和名称,其中首都需是国家名称的扩展 例如:答案中应该包括墨西哥城(Mexico City),因为它比墨西哥(Mexico)更长,而不应该将卢森堡(Luxembourg)包括在内,因为首都名与国家名相同
【参考答案】 select capital ,name from world where capital like concat('%',name,'%') and capital != name; 【3】The JOIN operation - SQLZOO第十三题 【题目】 查询每场比赛,每个球队的得分情况,按照以下格式显示
最后按照举办时间(mdate)、赛事编号(matchid)、队伍1(team1)和队伍2(team2)排序
【参考答案】 select ga.mdate ,ga.team1 ,sum(case when ga.team1=go.teamid then 1 else 0 end) score1 ,ga.team2 ,sum(case when ga.team2=go.teamid then 1 else 0 end) score2 from game ga left join goal go on ga.id = go.matchid group by ga.mdate,ga.team1,ga.team2 order by ga.mdate, go.matchid, ga.team1, ga.team2 【4】NSS Tutorial - SQLZOO第六题 【题目】 查询 来自'(8) Computer Science' 和'(H) Creative Arts and Design' 专业的学生,在回答问题22(Q22)时,表示强烈同意(A_STRONGLY_AGREE)的各专业人数占比 精确到个位数,不需要百分号 自行阅读题目涉及的表格哦~
【参考答案】 select subject , round(sum(A_STRONGLY_AGREE * response) / sum(response),0) from nss WHERE (question='Q22' and subject='(8) Computer Science' ) or (question='Q22' and subject = '(H) Creative Arts and Design') group by subject 主知识点九:窗口函数 【1】Window functions - SQLZOO第二题 【题目】 查询2017年选区为 'S14000024' 的所有候选人所在团体(party)和其得到选票数(votes)、还有候选人得票数在选区内对应的的排名 结果按团队party排序
【参考答案】 select party ,votes ,rank() over (order by votes desc) as posn from ge where constituency = 'S14000024' and yr = 2017 order by party 【2】Window functions - SQLZOO第四题 【题目】 查询2017年爱丁堡各个选区内各团队的排名情况 按照排名、选区排序 排名情况包括选区constituency,团队party,选票数votes,排名posn 已知爱丁堡选区编号为S14000021至S14000026
【参考答案】 select constituency ,party ,votes ,rank()over(partition by constituency order by votes desc) as posn from ge where constituency between 'S14000021' and 'S14000026' and yr = 2017 order by posn,constituency 【3】Window functions - SQLZOO第六题 【题目】 查询2017年在苏格兰的选区内(选区编号以S开头的)每个团体获得的席位数 选区候选人得票数为第1名,即获得该选区席位
【参考答案】 select party ,count(*) from ( select constituency ,party ,votes ,rank()over(partition by constituency order by votes desc) as posn from ge where constituency like 'S%' and yr = 2017 ) rk where rk.posn=1 group by rk.party 【4】Window LAG - SQLZOO第六题 【题目】 查询更新时间为2020年4月20日的国家名,确诊人数,确诊人数排名,死亡人数,死亡人数排名 按照确证人数降序排名
【参考答案】 select name ,confirmed ,rank()over(order by confirmed desc) rc ,deaths ,rank()over(order by deaths desc) deathrc from covid where whn = '2020-04-20' order by confirmed desc 【5】Window LAG - SQLZOO第二题 【题目】 修改给出的代码,查询三月份意大利每天的确诊人数和前一天的确诊人数 并按更新时间排序
【窗口函数LAG简介】 LAG(列名,n,m): 当前记录前面第n行记录的<列名>的值,没有则默认值为m;如果不带参数n,m,则查找当前记录前面第一行的记录<列名>的值,没有则默认值为null 【参考答案】 select name , day(whn) , confirmed ,lag(confirmed,1) over (partition by name order by whn) lag from covid where name = 'italy' and month(whn) = 3 order by whn 【6】Window LAG - SQLZOO第三题 【题目】 修改给出的代码,使用LAG函数查询三月份意大利每日新增确诊数
【参考答案】 select name , day(whn) , (confirmed -lag(confirmed, 1) over (partition by name order by whn)) new from covid where name = 'italy' and month(whn) = 3 order by whn 【7】Window LAG - SQLZOO第四题 这题务必认真复习!!! 【切换数据库操作】 找到页面最右上角的齿轮图标
点击图标
切换数据库引擎为MySQL
mysql支持函数date_format()和weekday(),但Microsoft SQl不支持
【题目】 查询意大利每周新增确诊数(显示每周一的数值 weekday(whn) = 0) 最后显示国家名,标准日期(2020-01-27),每周新增人数 按照更新时间排序
【参考答案】 select name , date_format(whn,'%Y-%m-%d') date , (confirmed - lag(confirmed,1)over(partition by name order by whn)) New from covid where name = 'Italy' and weekday(whn) = 0 order by whn
数据璐(大数据分析岗位推荐师)
———————————————— 版权声明:本文为CSDN博主「数据璐(大数据分析岗位推荐师)」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/shujlu0908/article/details/124297566