一、足球数据库

1.

image.png

  1. select matchid,player
  2. from goal
  3. where teamid = 'GER'

2.

image.png

  1. SELECT id,stadium,team1,team2
  2. FROM game
  3. where id = 1012

3.

image.png

  1. SELECT player,teamid,stadium,mdate
  2. FROM game JOIN goal ON (id=matchid)
  3. where teamid = 'GER'

4.

image.png

  1. select team1,team2,player
  2. from game join goal on id=matchid
  3. where player like 'Mario%'

5.image.png

  1. SELECT player, teamid, coach,gtime
  2. FROM goal as x
  3. JOIN eteam as y
  4. on x.teamid = y.id
  5. WHERE gtime<=10

6.

image.png

  1. select mdate,teamname
  2. from eteam as x
  3. join game as y
  4. on x.id = y.team1
  5. where coach = 'Fernando Santos'

7.

image.png

  1. select player
  2. from goal as x
  3. join game as y
  4. on x.matchid = y.id
  5. where stadium = 'National Stadium, Warsaw'

8.

image.png

题目要求:列出面对德国队有进球记录的球员姓名
思路拆解:

  1. 列出球员姓名:select name
  2. 面对德国队:
    1. 德国队的对手:teamid != ‘GER’
    2. 双方其中一队是德国队:team1 = ‘GER’ or team2 = ‘GER’

9.

image.png

  1. SELECT teamname, count(*)
  2. FROM eteam JOIN goal ON id=teamid
  3. GROUP BY teamname

10.

image.png

11.

image.png

12.

image.png

  1. SELECT matchid,mdate,count(*)
  2. FROM goal JOIN game ON id = matchid
  3. WHERE teamid = 'GER'
  4. GROUP BY matchid,mdate

13.image.png

  1. SELECT mdate,
  2. team1,
  3. sum(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) as score1,
  4. team2,
  5. sum(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) as score2
  6. FROM game JOIN goal ON matchid = id
  7. GROUP BY mdate,matchid,team1,team2

二、音乐数据库

三、more JOINT

6.

image.png

  1. select name
  2. from actor as x
  3. join casting as y on x.id= y.actorid
  4. join movie as z on z.id = y.movieid
  5. where title = 'Casablanca'

7.

image.png

  1. select name
  2. from actor as x
  3. join casting as y on x.id = y.actorid
  4. join movie as z on z.id = y.movieid
  5. where z.title = 'Alien'

8.

image.png

  1. select title
  2. from movie as x
  3. join casting as y on x.id = y.movieid
  4. join actor as z on z.id = y.actorid
  5. where name in ('Harrison Ford')

9.

image.png

  1. select title
  2. from movie as x
  3. join casting as y on x.id = y.movieid
  4. join actor as z on z.id = y.actorid
  5. where z.name = 'Harrison Ford' and y.ord != 1

10.

image.png

  1. select title,name
  2. from movie as x
  3. join casting as y on x.id = y.movieid
  4. join actor as z on z.id = y.actorid
  5. where x.yr = 1962 and ord = 1

11.

image.png

  1. SELECT yr,COUNT(title) FROM
  2. movie JOIN casting ON movie.id=movieid
  3. JOIN actor ON actorid=actor.id
  4. WHERE name='Rock Hudson'
  5. GROUP BY yr
  6. HAVING COUNT(title) > 2

12.

image.png

  1. SELECT title,name
  2. FROM movie as x
  3. join casting as y on x.id = y.movieid
  4. join actor as z on z.id = y.actorid
  5. WHERE movieid IN (
  6. SELECT movieid FROM actor
  7. join casting on id=actorid
  8. WHERE name='Julie Andrews') and ord = 1

13.

image.png

  1. select name
  2. from actor as x
  3. join casting as y on x.id = y.actorid
  4. join movie as z on z.id = y.movieid
  5. where ord = 1
  6. group by name
  7. having count(*) >= 15
  8. order by name

14.

image.png

  1. select title,count(name)
  2. from movie as x
  3. join casting as y on x.id = y.movieid
  4. join actor as z on z.id = y.actorid
  5. where yr = 1978
  6. group by title
  7. order by count(name) desc,title

15.

image.png

  1. select name
  2. from actor as x
  3. join casting as y on x.id = y.actorid
  4. where y.movieid in(
  5. select movieid
  6. from casting join actor on actorid = id
  7. where name = 'Art Garfunkel') and name != 'Art Garfunkel'