目录
Old JOIN Tutorial - The Table Tennis Olympics Database
The JOIN operation
JOIN and UEFA EURO 2012
1.Modify it to show the matchid and player name for all goals scored by Germany.
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 (id=matchid)
WHERE teamid='GER'
4.Show the team1, team2 and player for every goal scored by a player called Mario
SELECT team1, team2, player
FROM game JOIN goal ON (id=matchid)
WHERE 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 teamid=id
WHERE gtime<=10
6.List 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 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 matchid = id
WHERE (team1='GER' OR team2='GER') AND teamid <> 'GER'
9.Show teamname and the total number of goals scored.
SELECT teamname, COUNT(*)
FROM eteam JOIN goal ON id=teamid
GROUP BY teamname
10.Show the stadium and the number of goals scored in each stadium.
SELECT stadium, COUNT(*)
FROM game JOIN goal ON 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(*)
FROM game, goal
WHERE (team1 = 'POL' OR team2 = 'POL') and game.id=goal.matchid
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(*)
FROM game, goal
WHERE goal.teamid='GER' AND game.id=goal.matchid
GROUP BY matchid, mdate
13.List every match with the goals scored by each team as shown.
SELECT mdate, team1,
SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1, team2,
SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2
FROM game LEFT JOIN goal ON matchid = id
GROUP BY mdate, matchid, team1, team2
JOIN Quiz
1.D
2.C
3.A
4.A
5.B
6.C
7.B
Old JOIN Tutorial - The Table Tennis Olympics Database
1.Show the athelete (who) and the country name for medal winners in 2000.
SELECT who, country.name
FROM ttms JOIN country ON (ttms.country=country.id)
WHERE games = 2000
2.Show the who and the color of the medal for the medal winners from 'Sweden'.
SELECT who, color
FROM ttms JOIN country ON (ttms.country=country.id)
WHERE name='Sweden'
3.Show the years in which 'China' won a 'gold' medal.
SELECT games
FROM ttms JOIN country ON (ttms.country=country.id)
WHERE name='China' AND color='gold'
4.Show who won medals in the 'Barcelona' games.
SELECT who
FROM ttws JOIN games ON (ttws.games=games.yr)
WHERE city = 'Barcelona'
5.Show which city 'Jing Chen' won medals. Show the city and the medal color.
SELECT city, color
FROM ttws JOIN games ON (ttws.games=games.yr)
WHERE who='Jing Chen'
6.Show who won the gold medal and the city.
SELECT who, city
FROM ttws JOIN games ON (ttws.games=games.yr)
WHERE color='gold'
7.Show the games and color of the medal won by the team that includes 'Yan Sen'.
SELECT games, color
FROM team JOIN ttmd ON (team.id=ttmd.team)
WHERE name='Yan Sen'
8.Show the 'gold' medal winners in 2004.
SELECT name
FROM team JOIN ttmd ON (team.id=ttmd.team)
WHERE color='gold' AND games='2004'
9.Show the name of each medal winner country 'FRA'.
SELECT name
FROM team JOIN ttmd ON (team.id=ttmd.team)
WHERE color='gold' AND games='2004'
More JOIN operations
Movie database
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'.
SELECT DISTINCT name
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE casting.movieid=11768
7.Obtain the cast list for the film 'Alien'
SELECT DISTINCT name
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE movie.title='Alien'
8.List the films in which 'Harrison Ford' has appeared
SELECT DISTINCT movie.title
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE actor.name='Harrison Ford'
9.List the films where 'Harrison Ford' has appeared - but not in the starring role.
SELECT DISTINCT movie.title
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE actor.name='Harrison Ford' AND ord<>1
10.List the films together with the leading star for all 1962 films.
SELECT movie.title, actor.name
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE movie.yr=1962 AND casting.ord=1
11.Which were the busiest years for 'Rock Hudson', 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='Rock Hudson'
GROUP BY yr
HAVING COUNT(title) > 2
12.List the film title and the leading actor for all of the films 'Julie Andrews' played in.
SELECT movie.title, actor.name
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE movie.id IN (
SELECT DISTINCT movie.id
FROM casting JOIN actor ON (actor.id=casting.actorid)
JOIN movie ON (movie.id=casting.movieid)
WHERE name='Julie Andrews') AND casting.ord=1
13.Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
SELECT actor.name
FROM actor JOIN casting ON (actor.id=casting.actorid)
WHERE casting.ord=1
GROUP BY actor.name
HAVING COUNT(*)>=15
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 movie.title, COUNT(casting.actorid)
FROM movie JOIN casting ON (movie.id=casting.movieid)
WHERE movie.yr=1978
GROUP BY movie.title
ORDER BY COUNT(casting.actorid) DESC, movie.title
15.List all the people who have worked with 'Art Garfunkel'.
SELECT DISTINCT actor.name
FROM casting JOIN actor ON (casting.actorid=actor.id)
WHERE actor.name<>'Art Garfunkel' AND casting.movieid IN (
SELECT casting.movieid
FROM casting JOIN actor ON (casting.actorid=actor.id)
WHERE name='Art Garfunkel')
JOIN Quiz 2
1.C
2.E
3.C
4.B
5.D
6.C
7.B
Self JOIN
Edinburgh Buses
1.How many stops are in the database.
SELECT COUNT(*)
FROM stops
2.Find the id value for the stop 'Craiglockhart'
SELECT id
FROM stops
WHERE name='Craiglockhart'
3.Give the id and the name for the stops on the '4' 'LRT' service.
SELECT stops.id, stops.name
FROM route LEFT JOIN stops ON route.stop=stops.id
WHERE num='4' AND company='LRT'
4.The query shown gives the number of routes that visit either London Road (149) or Craiglockhart (53). Run the query and notice the two services that link these stops have a count of 2. Add a HAVING clause to restrict the output to these two routes.
SELECT company, num, COUNT(*)
FROM route
WHERE stop=149 OR stop=53
GROUP BY company, num
HAVING COUNT(num)=2
5.Execute the self join shown and observe that b.stop gives all the places you can get to from Craiglockhart, without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.
SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
WHERE a.stop=53 AND b.stop=149
6.The query shown is similar to the previous one, however by joining two copies of the stops table we can refer to stops by name rather than by number. Change the query so that the services between 'Craiglockhart' and 'London Road' are shown. If you are tired of these places try 'Fairmilehead' against 'Tollcross'
SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
WHERE stopa.name='Craiglockhart' AND stopb.name='London Road'
7.Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
SELECT DISTINCT a.company, a.num
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
WHERE a.stop=115 AND b.stop=137
8.Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
SELECT DISTINCT a.company, a.num
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
JOIN stops sa ON (a.stop=sa.id)
JOIN stops sb ON (b.stop=sb.id)
WHERE sa.name='Craiglockhart' AND sb.name='Tollcross'
9.Give a distinct list of the stops which may be reached from 'Craiglockhart' by taking one bus, including 'Craiglockhart' itself, offered by the LRT company. Include the company and bus no. of the relevant services.
SELECT sb.name, a.company, a.num
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
JOIN stops sa ON (a.stop=sa.id)
JOIN stops sb ON (b.stop=sb.id)
WHERE sa.name='Craiglockhart' AND a.company='LRT'
10.Find the routes involving two buses that can go from Craiglockhart to Lochend.Show the bus no. and company for the first bus, the name of the stop for the transfer, and the bus no. and company for the second bus.
SELECT a.num, a.company, sb.name, c.num, c.company
FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num)
JOIN route c ON (b.stop=c.stop)
JOIN route d ON (c.company=d.company AND c.num=d.num)
JOIN stops sa ON (a.stop=sa.id)
JOIN stops sb ON (b.stop=sb.id)
JOIN stops sc ON (c.stop=sc.id)
JOIN stops sd ON (d.stop=sd.id)
WHERE sa.name='Craiglockhart' AND sd.name='Lochend'
ORDER BY a.num, sb.name, c.num
Self JOIN Quiz
1.C
2.E
3.D