SQLZOO习题详解(08)——The JOIN operation

Posted 谷哥的小弟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLZOO习题详解(08)——The JOIN operation相关的知识,希望对你有一定的参考价值。


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

表及其关系

movie表如下:
在这里插入图片描述
在这里插入图片描述

actor表如下:
在这里插入图片描述
在这里插入图片描述

casting表如下:
在这里插入图片描述
在这里插入图片描述
表的关系如下:
在这里插入图片描述

链接地址

https://napier.sqlzoo.net/wiki/More_JOIN_operations

SQL练习

01、请查询1962首映的电影的id和title

SELECT id, title FROM movie WHERE yr=1962

02、请查询电影Citizen Kane’的首影年份

SELECT yr FROM movie WHERE title= 'Citizen Kane'

03、查询电影名以Star Trek开头的电影的id、title、yr

SELECT id, title, yr FROM movie WHERE title LIKE 'Star Trek%' ORDER BY yr

04、查询演员Glenn Close对应的编号id

SELECT id FROM actor WHERE name = 'Glenn Close' 

05、查询电影Casablanca对应的编号id

SELECT id FROM movie WHERE title LIKE 'Casablanca'

06、请查询参演电影编号为11768的电影的演员名

SELECT name
FROM actor INNER JOIN casting ON actor.id = casting.actorid
WHERE casting.movieid = 11768
SELECT name
FROM actor
WHERE id IN (
	SELECT actorid
	FROM movie INNER JOIN casting ON movie.id = casting.movieid
	WHERE movie.id = 11768
)

07、查询电影名为Alien的电影的演员

SELECT name FROM actor
WHERE id IN (
	SELECT actorid
	FROM movie INNER JOIN casting ON movie.id = casting.movieid
	WHERE movie.id = (
		SELECT id
		FROM movie
		WHERE title LIKE 'Alien'
	)
)

08、查询Harrison Ford参演过的所有电影的名称

SELECT title
FROM movie JOIN casting ON movie.id = casting.movieid
WHERE casting.actorid = (
	SELECT id
	FROM actor
	WHERE name = 'Harrison Ford'
)

09、查询Harrison Ford参演过但是不是第一主角的电影的名称

SELECT title
FROM movie JOIN casting ON (movie.id= casting.movieid)
WHERE casting.actorid =(
  SELECT id 
  FROM actor
  WHERE name = 'Harrison Ford') AND casting.ord!=1

10、请查询1962年首映的电影以及它的第一主角的名字

SELECT m.title, a.name
FROM casting AS c 
INNER JOIN movie AS m ON (m.id= c.movieid)
INNER JOIN actor AS a ON (a.id= c.actorid)
WHERE c.ord = 1 AND yr = 1962

11、请查询演员Rock Hudson年度参演电影数量大于2部的年份及该年份参演的电影的具体数量

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

在这里插入图片描述
12、请查询Julie Andrews参演过的电影的名称已经该电影的第一主角姓名

SELECT title, name
FROM casting
	JOIN movie ON movie.id = casting.movieid
	JOIN actor ON casting.actorid = actor.id
WHERE movieid IN (
		SELECT movieid
		FROM casting
		WHERE actorid = (
			SELECT id
			FROM actor
			WHERE name = 'Julie Andrews'
		)
	)
	AND ord = 1;

13、查询至少15次当第一主角的演员的名字并按照名字排序

SELECT name
FROM actor
	JOIN casting ON casting.actorid = actor.id
	JOIN movie ON movie.id = casting.movieid
WHERE casting.ord = 1
GROUP BY actor.name
HAVING count(title) >= 15
ORDER BY actor.name

14、请查询1978首映的电影。请将结果按照电影参演人数和电影名字排序

SELECT title, COUNT(actorid) AS num
FROM casting JOIN movie ON id = movieid
WHERE yr= 1978
GROUP BY movieid, title
ORDER BY num DESC, title;
SELECT title, count(actorid)
FROM movie INNER JOIN casting ON casting.movieid = movie.id
WHERE yr = '1978'
GROUP BY title
ORDER BY count(actorid) DESC, title

15、请查询与Art Garfunkel合作过的演员的名字

SELECT name
FROM actor INNER JOIN casting ON actor.id = casting.actorid
WHERE movieid IN (
		SELECT movieid
		FROM casting
		WHERE actorid IN (
			SELECT id
			FROM actor
			WHERE name = 'Art Garfunkel'
		)
	)
	AND name != 'Art Garfunkel'

SELECT name
FROM actor INNER JOIN casting ON actor.id = casting.actorid
WHERE movieid IN (
		SELECT movieid
		FROM casting INNER JOIN actor ON casting.actorid = actor.id
		WHERE name = 'Art Garfunkel'
	)
	AND name != 'Art Garfunkel'

以上是关于SQLZOO习题详解(08)——The JOIN operation的主要内容,如果未能解决你的问题,请参考以下文章

SQLZOO习题详解(09)——The JOIN operation

SQLZOO习题详解(10)——Self join

SQL练习-SQLZOO SQLZOO:The JOIN operation

SQL练习-SQLZOO SQLZOO:The JOIN operation

SQLZOO习题详解(05)——SELECT within SELECT

SQLZOO习题详解(06)——SUM and COUNT