SQLZOO join进阶练习

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

一、The JOIN operation

SQLZOO join进阶练习_第1张图片

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

SQLZOO join进阶练习_第2张图片

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

你可能感兴趣的:(SQLZOO join进阶练习)