您的位置:首页 > 数据库 > SQL

SQLZOO练习笔记-MORE JOIN OPERATIONS

2020-02-04 01:35 267 查看

题目地址:MORE JOIN OPERATIONS

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

SELECT id, title FROM movie
WHERE yr=1962

  1. 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

  1. What id number does the actor ‘Glenn Close’ have?

SELECT id FROM actor
WHERE name = ‘Glenn Close’

  1. What is the id of the film ‘Casablanca’

SELECT id FROM movie
WHERE title = ‘Casablanca’

  1. Obtain the cast list for ‘Casablanca’.
    Use movieid=11768, (or whatever value you got from the previous question)

SELECT name FROM actor
JOIN casting ON actorid = id
WHERE movieid = 11768

  1. Obtain the cast list for the film ‘Alien’

SELECT name FROM actor
JOIN casting ON actorid = id
WHERE movieid IN (SELECT id FROM movie
WHERE title = ‘Alien’)

  1. List the films in which ‘Harrison Ford’ has appeared

SELECT title FROM movie
WHERE id IN (SELECT movieid FROM casting
WHERE actorid IN (select id from actor
WHERE name = ‘Harrison Ford’))

  1. 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 movie
WHERE id IN (SELECT movieid FROM casting
WHERE actorid IN (SELECT id FROM actor
WHERE name = ‘Harrison Ford’
AND ord <> 1))

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

  1. SELECT title, name FROM
    (SELECT title, actorid FROM
    (SELECT id, title, actorid, ord FROM
    movie LEFT JOIN casting ON movie.id = movieid
    WHERE yr = 1962) c
    WHERE ord = 1) d
    LEFT JOIN actor ON actor.id = actorid

方法二

SELECT title, name FROM
(movie LEFT JOIN casting ON movie.id = movieid)
LEFT JOIN actor ON actorid = actor.id
WHERE yr = 1962 AND ord = 1

可以看到第二种方法代码要简洁很多,因为一次性left join了三张表格,再进行条件筛选。这里使用的left join语法类似于:
SELECT col1, col2, … FROM (t1 LEFT JOIN t2 on t1.x1 = t2.x2) LEFT JOIN t3 ON t2.y2 = t3.y2
此外,这里输出的代码直接按title排序,与原答案的输出顺序不一样,但内容是一样的。

11.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(yr) FROM (SELECT * FROM casting
LEFT JOIN actor ON actorid = actor.id
WHERE NAME = ‘John Travolta’) c
LEFT JOIN movie ON movieid = movie.id
GOURP BY yr HAVING count(yr) > 2

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

SELECT title, name FROM movie
JOIN (
SELECT * FROM actor
LEFT JOIN casting ON actorid = actor.id
WHERE movieid in (SELECT movieid FROM casting
LEFT JOIN actor ON actorid = actor.id
WHERE NAME = ‘Julie Andrews’)
AND ord = 1) c
ON movie.id = movieid

这里我之前想的方向是, 分别用两个select语句求出leading actor和film title,再将两个列拼接起来,但是好像比较麻烦……没有现在这个简洁。

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

SELECT name, COUNT(name) FROM actor
LEFT JOIN casting ON actor.id = actorid
GROUP BY name HAVING COUNT(name) >= 30

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

SELECT title, count(title) FROM movie
LEFT JOIN casting ON movie.id = movieid
WHERE yr = 1978
GROUP BY title
ORDER BY COUNT(title) DESC, TITLE

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

SELECT name FROM actor
LEFT JOIN casting ON actor.id = actorid
WHERE movieid IN (
SELECT movieid FROM casting
LEFT JOIN actor ON actor.id = actorid
WHERE name = ‘Art Garfunkel’)
AND name <> ‘Art Garfunkel’

  • 点赞
  • 收藏
  • 分享
  • 文章举报
yizhu007_ 发布了7 篇原创文章 · 获赞 0 · 访问量 538 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: