
DAY5 练习
The JOIN operation/zh:https://sqlzoo.net/wiki/The_JOIN_operation/zh
Music Tutorial/zh:https://sqlzoo.net/wiki/Music_Tutorial/zh
JOIN Quiz:https://sqlzoo.net/wiki/JOIN_Quiz
More JOIN operations/zh:https://sqlzoo.net/wiki/More_JOIN_operations/zh
JOIN Quiz 2:https://sqlzoo.net/wiki/JOIN_Quiz_2
--The JOIN operation/zh--1selectmatchid,playerfrom goalwhere teamid = 'GER';--2selectid,stadium,team1,team2from gamewhere id = '1012';--3selectgoal.player,goal.teamid,game.stadium,game.mdatefrom game join goal on game.id = goal.matchidwhere teamid = 'GER';--4selectgame.team1,game.team2,goal.playerfrom game join goal on game.id = goal.matchidwhere goal.player like 'Mario%';--5selectgoal.player,goal.teamid,eteam.coach,goal.gtimefrom goal join game on game.id = goal.matchidjoin eteam on goal.teamid = eteam.idwhere gtime <= 10;--6selectgame.mdate,eteam.teamnamefrom game join eteam on game.team1 = eteam.idwhere eteam.coach = 'Fernando Santos';--7select goal.playerfrom game join goal on game.id = goal.matchidwhere stadium = 'National Stadium, Warsaw';--8select distinct goal.playerfrom goal join game on goal.matchid = game.idwhere teamid != 'GER'and (game.team1 = 'GER'or game.team2 = 'GER');/*这道题不要忘记排除进球的GER本身*/--9selecteteam.teamname,count(goal.gtime)from goal join eteam on goal.teamid = eteam.idgroup by eteam.teamname;--10selectgame.stadium,count(goal.gtime)from goal join game on game.id = goal.matchidgroup by game.stadium;--11selectgoal.matchid,game.mdate,count(goal.gtime)from goal join game on game.id = goal.matchidwhere team1 = 'POL'or team2 = 'POL'group by matchid,mdate;--12selectgoal.matchid,game.mdate,count(goal.gtime)from game join goal on game.id = goal.matchidwhere goal.teamid = 'GER'group by goal.matchid,game.mdate;--13selectgame.mdate,game.team1,sum(case when goal.teamid = game.team1 then 1 else 0 end) as score1,game.team2,sum(case when goal.teamid = game.team2 then 1 else 0 end) as score2from game left join goal on game.id = goal.matchidgroup by game.mdate,goal.matchid,game.team1,game.team2order by game.mdate,goal.matchid,game.team1,game.team2;/*这道题注意题干中Sort和Notice in the query given every goal is listed的要求,因此使用了group、order、left join*/--Music Tutorial/zh--1selectalbum.title,album.artistfrom album join track on album.asin = track.albumwhere track.song = 'Alison';--2select album.artistfrom album join track on album.asin = track.albumwhere track.song = 'Exodus';--3select track.songfrom album join track on album.asin = track.albumwhere album.title = 'Blur';/*读了题干几遍才明白Blur指的是title*/--4selectalbum.title,count(track.song)from album join track on album.asin = track.albumgroup by album.title;--5selectalbum.title,count(track.song)from album join track on album.asin = track.albumwhere song like '%Heart%'group by album.title;--6select track.songfrom album join track on album.asin = track.albumwhere album.title = track.song;--7select titlefrom albumwhere title = artist;--8selecttrack.song,count(distinct album)from album join track on album.asin = track.albumgroup by track.songhaving count(distinct album) > 2;--9selectalbum.title,album.price,count(track.song)from album join track on album.asin = track.albumgroup by album.title,album.pricehaving album.price/count(track.song) < 0.5;--10selectalbum.title,count(track.song) as numfrom album join track on album.asin = track.albumgroup by album.titleorder by num DESC;/*这道题有报错,原因未知*/--More JOIN operations/zh--1selectid,titlefrom moviewhere yr = 1962;--2select yrfrom moviewhere title = 'Citizen Kane';--3selectid,title,yrfrom moviewhere title like 'Star Trek%'order by yr;--4select titlefrom moviewhere id in ('11768','11955','21191');--5select idfrom actorwhere name = 'Glenn Close';--6select idfrom moviewhere title = 'Casablanca';--7selectactor.namefrom actor join casting on actor.id = casting.actoridwhere movieid = 11768;--8select actor.namefrom casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere movie.title = 'Alien';--9select movie.titlefrom casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere actor.name = 'Harrison Ford';--10select movie.titlefrom casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere actor.name = 'Harrison Ford'and casting.ord != 1;--11selectmovie.yr,count(movie.title)from casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere actor.name = 'John Travolta'group by yrhaving count(title) >= all(select count(title)from casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere actor.name = 'John Travolta'group by yr);/*第二种解法*/selectmovie.yr,count(movie.title)from casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere actor.name = 'John Travolta'group by yrhaving count(movie.title) = (select max(c)from (select yr, count(movie.title) as cfrom casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere actor.name = 'John Travolta'group by yr) as ps);/*注意每一个派生出来的表都必须有一个自己的别名*/--13selectmovie.title,actor.namefrom casting join movie on casting.movieid = movie.idjoin actor on casting.actorid = actor.idwhere casting.movieid in (select casting.movieidfrom casting join actor on casting.actorid = actor.idwhere actor.name = 'Julie Andrews')and ord = 1;--14select actor.namefrom actor join casting on casting.actorid = actor.idwhere casting.ord = 1group by actor.namehaving count(actor.name) >= 30order by actor.name;--15selectmovie.title,count(*)from casting join movie on casting.movieid = movie.idwhere movie.yr = 1978group by movie.titleorder by count(*) desc,title;/*count(*)和count(字段名)的区别:前者对行的数目进行计算,包含null,后者对特定的列的值具有的行数进行计算,不包含null,得到的结果将是除去值为null和重复数据后的结果。*/--16select distinct actor.namefrom actor join casting on casting.actorid = actor.idwhere casting.movieid in (select casting.movieidfrom actor join casting on casting.actorid = actor.idwhere actor.name = 'Art Garfunkel')and actor.name != 'Art Garfunkel';/*思路:首先求出Art Garfunkel演过的所有电影,其次查询所有演员,最后排除Art Garfunkel*/
end
文章转载自EGG IXD,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




