Sqlzoo习题练习:More JOIN operations

下面会涉及到更多连接的概念。数据库由三个表组成:movie , actor 和 casting以及三个表之间的关系。

下面为More JOIN 习题内容:

--#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%';

--#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'.what is a cast list?The cast list is the names of the actors who were in the movie.Use movieid=11768, (or whatever value you got from the previous question)*/

SELECT actor.name

FROM actor JOIN casting ON (actor.id=casting.actorid)

WHERE casting.movieid = 11768;

--#7/*Obtain the cast list for the film 'Alien'*/

SELECT actor.name

FROM actor JOIN casting

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 movie.title

FROM actor JOIN casting

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. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]*/

SELECT movie.title

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE actor.name = 'Harrison Ford' AND casting.ord != 1;

--#10/*List the films together with the leading star for all 1962 films.*/

SELECT movie.title,actor.name

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE movie.yr = 1962

AND casting.ord = 1;

知识点:MAX() 函数

MAX 函数返回一列中的最大值。NULL 值不包括在计算中。

MAX() 语法

SELECT MAX(column_name) FROM table_name

--#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(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

);

--#12

/*

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

Did you get "Little Miss Marker twice"?

Julie Andrews starred in the 1980 remake of Little Miss Marker and not the original(1934).

Title is not a unique field, create a table of IDs in your subquery

*/

SELECT DISTINCT m.title,a.name

FROM (SELECT movie.*

FROM actor JOIN casting

ON (actor.id=casting.actorid)

JOIN movie

ON (movie.id = casting.movieid)

WHERE actor.name = 'Julie Andrews') AS m

JOIN (SELECT actor.*,casting.movieid AS movieid

FROM actor JOIN casting

ON (actor.id=casting.actorid)

WHERE casting.ord = 1) AS a

ON m.id = a.movieid

ORDER BY m.title;

--#13

/*

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

*/

SELECT actor.name

FROM actor

JOIN casting

on (actor.id = casting.actorid)

WHERE casting.ord = 1

GROUP BY actor.name

HAVING COUNT(*) >= 30;

--#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(actorid) AS cast_actors

FROM movie

JOIN casting

ON movie.id = casting.movieid

JOIN actor

ON actor.id = casting.actorid

WHERE movie.yr = 1978

GROUP BY movie.title

ORDER BY cast_actors DESC,movie.title;

--#15

/*

List all the people who have worked with 'Art Garfunkel'.

*/

SELECT a.name

FROM (SELECT movie.*

FROM movie

JOIN casting

ON casting.movieid = movie.id

JOIN actor

ON actor.id = casting.actorid

WHERE actor.name = 'Art Garfunkel') AS m

JOIN (SELECT actor.*, casting.movieid

FROM actor

JOIN casting

ON casting.actorid = actor.id

WHERE actor.name != 'Art Garfunkel') as a

ON m.id = a.movieid;

mysql begin operations_MySQL入门(七):More JOIN operations相关推荐

  1. MySQL入门 (七) : 储存引擎与资料型态

    1 表格与储存引擎 表格(table)是资料库中用来储存纪录的基本单位,在建立一个新的资料库以后,你必须为这个资料库建立一些储存资料的表格: 每一个资料库都会使用一个资料夹,这些资料库资料夹用来储存所 ...

  2. sqlzoo第七关More JOIN operations

    第七关 More JOIN operations 电影数据库 表1 电影表(movie) id(编号) title(电影名) yr(年份) director(导演) budget(制作费) gross ...

  3. 5、MySQL 七种 Join 形式分析

    5.MySQL 七种 Join 形式分析 5.1.建表 CREATE TABLE `t_dept` (`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,`deptName ...

  4. 好程序员Java分享MySQL之SQL入门(一)

    好程序员Java分享MySQL之SQL入门(一)前言:各种版本的数据库中,有一种通用的语言用于管理数据库中的数据,它就是SQL,本章我们将学习基本的SQL语句. SQL的概述 Structured Q ...

  5. 好程序员分享MySQL之SQL入门(一)

    好程序员分享MySQL之SQL入门(一)前言:各种版本的数据库中,有一种通用的语言用于管理数据库中的数据,它就是SQL,本章我们将学习基本的SQL语句. SQL的概述 Structured Query ...

  6. ROS入门七 机器人建模——URDF

    ROS入门七 机器人建模--URDF urdf ufdf介绍 语法 创建机器人URDF模型 创建机器人描述功能包 创建URDF模型 在rviz中显示模型 改进URDF模型 添加物理和碰撞属性 使用xa ...

  7. torch(七)、Math operations(2)

    参考 torch(七).Math operations(2) - 云+社区 - 腾讯云 目录 Spectral Ops torch.fft(input, signal_ndim, normalized ...

  8. Mysql进阶学习(七)联合查询与DML语言

    Mysql进阶学习(七)联合查询与DML语言 进阶9:联合查询 语法: 特点:★ 案例 DML语言 1.插入语句(insert) 方式一:经典的插入 1.1.插入的值的类型要与列的类型一致或兼容 1. ...

  9. Mysql数据库基础入门教程

    Mysql数据库基础入门教程 课程链接:https://www.bilibili.com/video/BV1Qb411x7Yc?p=1 2022/1/22start 一.数据库简介 1.什么是数据库? ...

最新文章

  1. 当代的设计潮流是什么_12月,潮流咖的出行攻略!
  2. 牛客多校6 - Josephus Transform(线段树求k-约瑟夫环+置换群的幂)
  3. 【转】有限状态机用法教程
  4. 酱茄企业官网多端开源小程序源码 v1.0.0
  5. 自制反汇编逆向分析工具 迭代第六版本 (一)
  6. Java Swing框架实战
  7. Ztree加载完成后显示勾选节点
  8. 【ASP.NET 问题】IIS发布网站后出现 “处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误“的解决办法
  9. VMware vSphere 5.5的12个更新亮点(1)
  10. 其实我只想设置客户端实现跨域请求
  11. 关于防范ONION勒索软件病毒攻击的解决办法
  12. KT148A语音芯片ic的软件参考代码C语言,一线串口
  13. Redis高级之——redis-trib.rb命令详解
  14. Realtek 1296 (RTD1296) OpenWRT Android 双系统全功能开发板
  15. Unity连接MySQL数据库方法整合
  16. 主干光缆线路的组网结构
  17. Altium Designer 19 安装方法
  18. 对百度Bingo算法的猜测
  19. 诺丁汉大学高级计算机科学,诺丁汉大学高级计算机科学研究生语言及申请要求-费用-课程设置...
  20. 对物联网的感悟_请谈谈你对物联网的看法?

热门文章

  1. 重磅!AWS任命张文翊女士为全球副总裁及大中华区执行董事!
  2. 云漫圈 | 革命版互联网公司虐恋大戏,周一拿去怼业务!
  3. python启蒙视频_python启蒙阶段
  4. usleep延时0.毫秒_【进阶】用swoole实现订单的延时处理(自动取消,还原库存等)...
  5. python随机森林特征重要性_Python中随机森林回归的特征重要性
  6. JsonData工具类
  7. 获取本地ip地址适用于windows和Linux环境
  8. Springboot整合Quartz集群部署以及配置Druid数据源
  9. 解决idea修改html、js、css后,浏览器不能同步加载
  10. SpringBoot Mybatisplus 多数据源使用