1.列出1962年首影的電影,[顯示 id, title] SELECT id,title FROM movie WHERE yr=1962
1 2 3
2.電影大國民'Citizen Kane'的首影年份。 select yr from movie wheretitle = 'Citizen Kane'
1 2 3 4
3.列出全部Star Trek星空奇遇記系列的電影,包括id, title和yr(此系統電影都以Star Trek為電影名稱的開首)。按年份順序排列。 select id,title,yr from movie wheretitlelike'Star Trek%' orderby yr
1 2 3
4.id是11768, 11955, 21191的電影是什麼名稱? select title from movie where id in (11768,11955,21191)
1 2 3
5.女演員'Glenn Close'的編號 id是什麼? select id from actor wherename = 'Glenn Close'
1 2 3
6.電影北非諜影'Casablanca'的編號id是什麼? select id from movie wheretitle = 'Casablanca'
1 2 3 4
7.列出電影北非諜影'Casablanca'的演員名單。 selectnamefrom actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) where title = 'Casablanca'
1 2 3 4
8.顯示電影異型'Alien'的演員清單。 selectnamefrom actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) where title = 'Alien'
1 2 3 4
9.列出演員夏里遜福'Harrison Ford'曾演出的電影。 select title from actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) wherename = 'Harrison Ford'
1 2 3 4
10.列出演員夏里遜福'Harrison Ford'曾演出的電影,但他不是第1主角。 select title from actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) wherename = 'Harrison Ford'and ord != 1
1 2 3 4
11.列出1962年首影的電影及它的第1主角。 select title,namefrom actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) where yr = 1962and ord = 1
More Difficult
1 2 3 4 5 6 7 8 9 10 11 12 13
12.尊·特拉華達'John Travolta'最忙是哪一年? 顯示年份和該年的電影數目。 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 )
1 2 3 4 5 6 7 8 9 10 11
13.列出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱及其第1主角。 select title,namefrom actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) where ord = 1and movieid in (select movieid from actor join casting on (actor.id=actorid) join movie on (movie.id=movieid) wherename = 'Julie Andrews')
应该用movieid而不用title,因为title可能会重复
1 2 3 4 5 6
14.列出按字母順序,列出哪一演員曾作30次第1主角。 selectnamefrom actor join casting on (actor.id=actorid) whereord = 1 groupby actorid,name havingcount(movieid) >= 30 orderbyname
1 2 3 4 5 6 7
15.列出1978年首影的電影名稱及角色數目,按此數目由多至少排列。 selecttitle,count(actorid) from casting join movie on (movie.id=movieid) where yr = 1978 groupby movieid,title orderbycount(actorid) desc
英文版最后的排序条件还会加上title,中文版一直通不过...
1 2 3 4 5 6 7
16.列出曾與演員亞特·葛芬柯'Art Garfunkel'合作過的演員姓名。 selectnamefrom actor join casting on (actor.id=actorid) wherename != 'Art Garfunkel'and movieid in (select movieid from actor join casting on (actor.id=actorid) wherename = 'Art Garfunkel')
Quiz
1.Select the statement which lists the unfortunate directors of the movies which have caused financial loses (gross < budget) Answer:
1 2 3
SELECTname FROM actor INNERJOIN movie ON actor.id = director WHERE gross < budget
2.Select the correct example of JOINing three tables Answer:
1 2 3
SELECT * FROM actor JOIN casting ON actor.id = actorid JOIN movie ON movie.id = movieid
3.Select the statement that shows the list of actors called’John’by order of number of movies in which they acted Answer:
1 2 3 4
SELECTname, COUNT(movieid) FROM casting JOIN actor ON actorid=actor.id WHEREnameLIKE'John %' GROUPBYnameORDERBY2DESC
4.Select the result that would be obtained from this code:
1 2 3 4
SELECT title FROM movie JOIN casting ON (movieid=movie.id) JOIN actor ON (actorid=actor.id) WHEREname='Paul Hogan'ANDord = 1
Answer:
5.Select the statement that lists all the actors that starred in movies directed by Ridley Scott who has id 351 Answer:
1 2 3 4
SELECTname FROM movie JOIN casting ON movie.id = movieid JOIN actor ON actor.id = actorid WHEREord = 1AND director = 351
6.There are two sensible ways to connect movie and actor. They are: Answer:
1 2
link the director column in movies withthe primary key in actor connect the primary keysof movie and actor via the casting table
7.Select the result that would be obtained from the following code:
1 2 3
SELECT title, yr FROM movie, casting, actor WHERE name='Robert De Niro'ANDmovieid=movie.id ANDactorid=actor.id AND ord = 3