SQLZOO join进阶练习

SQLZOO练习网址: https://sqlzoo.net/wiki/The_JOIN_operation

一、The JOIN operation

1.Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: teamid = 'GER'

select matchid,player from goal where teamid='GER'

2. Show id, stadium, team1, team2 for just game 1012

select id,stadium,team1,team2 from game  where id=1012

3. Modify it to show the player, teamid, stadium and mdate for every German goal.

select player,teamid,stadium,mdate 
from game  join goal on (game.id=matchid)
where goal.teamid = 'GER'

4. Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

select team1,team2,player 
from game join goal on (game.id=matchid)
where goal.player like'Mario%'

5.Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

select player, teamid,coach,gtime
from goal join eteam on (goal.teamid=eteam.id)
where goal.gtime <= 10

6. List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.

select mdate,teamname
from game join eteam on (team1=eteam.id)  
where coach='Fernando Santos'

7. List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'

select player 
from game join goal on(game.id=goal.matchid)
where game.stadium ='National Stadium, Warsaw'

8. Instead show the name of all players who scored a goal against Germany.

select distinct player 
from game join goal on (game.id=matchid) 
where (game.team1='GER' or game.team2='GER')and(teamid != 'GER')

9. Show teamname and the total number of goals scored.

select teamname, count(gtime)
from eteam join goal on(eteam.id=goal.teamid)
group by teamname

10. Show the stadium and the number of goals scored in each stadium.

select stadium ,count(gtime)
from game join goal on (game.id = matchid)
group by  stadium

11. For every match involving 'POL', show the matchid, date and the number of goals scored.

select matchid,mdate, count(teamid)
from game join goal on(game.id=goal.matchid)
where team1='POL' or team2='POL'
group by matchid,mdate

12. For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'

select matchid,mdate,count(teamid)
from game join goal on(game.id=goal.matchid)
where teamid='GER'
group by matchid,mdate

13.List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.

Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. Sort your result by mdate, matchid, team1 and team2.

select game.mdate,team1,
sum(case when teamid = team1 then 1  else 0 end) as score1,team2,
sum(case when teamid = team2 then 1 else 0 end) as score2 
from game left join goal  on(game.id = matchid) 
group by mdate, matchid, team1, team2

二、More JOIN operations

This tutorial introduces the notion of a join. The database consists of three tables movie , actor and casting

1. List the films where the yr is 1962 [Show id, title]

select id, title  from movie  where yr=1962

2. Give year of 'Citizen Kane'.

select yr from movie where title='Citizen Kane'

3. List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.

select id,title,yr  from movie  where title like'%Star Trek%'  order by yr

4. What id number does the actor 'Glenn Close' have?

select id from actor where name='Glenn Close'

5. What is the id of the film 'Casablanca'

select id from movie where title='Casablanca'

6. Obtain the cast list for 'Casablanca'.

what is a cast list?

Use movieid=11768, (or whatever value you got from the previous question)

select name  from actor join casting on (actorid=actor.id) where movieid=11768

7. Obtain the cast list for the film 'Alien'

select name from casting join actor on(actor.id=actorid) 
join movie on (movieid=movie.id)
where title ='Alien'

8. List the films in which 'Harrison Ford' has appeared

select title from casting join actor on(actor.id=actorid)
join movie on(movieid=movie.id)
where actor.name='Harrison Ford'

9. List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]

select title from casting join actor on(actor.id=actorid)
join movie on(movieid=movie.id)
where actor.name='Harrison Ford' and casting.ord !=1

10. List the films together with the leading star for all 1962 films.

select distinct title,name from casting 
join actor on(actor.id=actorid)  join movie on(movieid=movie.id)
where movie.yr='1962' and ord=1

  1. Which were the busiest years for 'John Travolta', show the year and the number of movies he made each year for any year in which he made more than 2 movies.
SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
where name='John Travolta'
GROUP BY yr
HAVING COUNT(title)=(SELECT MAX(c) FROM
(SELECT yr,COUNT(title) AS c FROM
   movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr) AS t
)

12. List the film title and the leading actor for all of the films 'Julie Andrews' played in.

Did you get "Little Miss Marker twice"?

select distinct(title),name from casting 
join actor on(actor.id=actorid)
join movie on(movieid=movie.id) and ord=1
where movieid in (select movieid 
              from actor join casting on (actor.id=casting.actorid) 
                where actor.name='Julie Andrews' )

13. Obtain a list, in alphabetical order, of actors who've had at least 30 starring roles.

select name from casting 
join actor on(actor.id=actorid)  
join movie on(movie.id=movieid)  
where casting.ord=1
group by name
having count(movieid)>=30
order by actor.name

14. List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

select title,count(actorid) from casting 
join actor on(actor.id=actorid)
join movie on(movie.id=movieid)  
where yr=1978
group by title
order by count(actorid) desc ,movie.title

15. List all the people who have worked with 'Art Garfunkel'.

select name from casting 
join actor on(actor.id=actorid) join movie on(movie.id=movieid) 
where movieid in(select movieid 
from casting join actor on(actor.id=casting.actorid) and name='Art Garfunkel') 
and name !='Art Garfunkel'

三、Using Null

The school includes many departments. Most teachers work exclusively for a single department. Some teachers have no department.

1. List the teachers who have NULL for their department.

select name from teacher where dept is null

2. Note the INNER JOIN misses the teachers with no department and the departments with no teacher.

select teacher.name, dept.name
from teacher inner join dept on (teacher.dept=dept.id)

3. Use a different JOIN so that all teachers are listed.

select teacher.name,dept.name  from teacher left join dept on(teacher.dept=dept.id)

4. Use a different JOIN so that all departments are listed.

select teacher.name,dept.name  from teacher right join dept on(teacher.dept=dept.id)

5. Use COALESCE to print the mobile number. Use the number '07986 444 2266' if there is no number given. Show teacher name and mobile number or '07986 444 2266'

select name,COALESCE(mobile,'07986 444 2266') from teacher

6. Use the COALESCE function and a LEFT JOIN to print the teacher name and department name. Use the string 'None' where there is no department.

select teacher.name,coalesce(dept.name,'None')
from teacher left join dept on dept.id = teacher.dept

7. Use COUNT to show the number of teachers and the number of mobile phones.

select  count(teacher.name), count(mobile)  from teacher  

8. Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed.

select dept.name,count(teacher.name)
from teacher right join dept on(teacher.dept=dept.id)
group by dept.name

9. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise.

select name,
case when dept in(1,2) then 'Sci'
else 'Art' end
from teacher

10. Use CASE to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise.

select name,
case when dept in(1,2) then 'Sci'
when dept=3 then 'Art'
else 'None' end
from teacher
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,294评论 0 10
  • 我会表达感受吗?看完本章关于感受的描述,我觉得平时的自己更多的是在表达情绪,用“面部表情”、用“声音”、用“肢体语...
    chunyan阅读 304评论 0 1
  • 在韩国,大家都对耗油低的IONIQ期待值更高,而在日本新款普锐斯汽车却十分受欢迎。大众对混动汽车的关注度越来越高。...
    969227e884b9阅读 642评论 0 0
  • 2018-09-18 今天是我在第一个公司的最后一天,昨天和经理说要离职,今天就能走,虽然这是我自己提的,但这么...
    浮生_若梦_阅读 43评论 0 0