SQL练习六--More JOIN operations
movie
| Field name | Type | Notes |
|---|---|---|
| id | INTEGER | An arbitrary unique identifier |
| title | CHAR(70) | The name of the film - usually in the language of the first release. |
| yr | DECIMAL(4) | Year of first release. |
| director | INT | A reference to the actor table. |
| budget | INTEGER | How much the movie cost to make (in a variety of currencies unfortunately). |
| gross | INTEGER | How much the movie made at the box office. |
example of Movie
| id | title | yr | director | budget | gross |
|---|---|---|---|---|---|
| 10003 | "Crocodile" Dundee II | 1988 | 38 | 15800000 | 239606210 |
| 10004 | 'Til There Was You | 1997 | 49 | 10000000 |
actor
| Field name | Type | Notes |
|---|---|---|
| id | INTEGER | An arbitrary unique identifier |
| name | CHAR(36) | The name of the actor (the term actor is used to refer to both male and female thesps.) |
example of actor
| id | name |
|---|---|
| 20 | Paul Hogan |
| 50 | Jeanne Tripplehorn |
casting
| Field name | Type | Notes |
|---|---|---|
| movieid | INTEGER | A reference to the movie table. |
| actorid | INTEGER | A reference to the actor table. |
| ord | INTEGER | The ordinal position of the actor in the cast list. The
star of the movie will have ord value 1 the co-star will have value 2, ... |
example of casting
| movieid | actorid | ord |
|---|---|---|
| 10003 | 20 | 4 |
| 10004 | 50 | 1 |
1、1962 movies(1962年电影)
List the films where the yr is 1962 [Show id, title]
获取1962年上映的电影id和名称。
SELECT id, title
FROM movie
WHERE yr=1962;
2、When was Citizen Kane released?(公民凯恩什么时候上映的)
Give year of 'Citizen Kane'.
获取《公民凯恩》上映的年份。
select yr
from movie
where title='Citizen Kane';
3、Star Trek movies(星际迷航系列电影)
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.
获取《星际迷航》系列的id,电影名称,上映年份;按照年份生序排名。
select id,title,yr
from movie
where title LIKE'%Star Trek%'
order by yr;
4、id for actor Glenn Close(演员Glenn Close的id)
What id number does the actor 'Glenn Close' have?
select id
from actor
where name='Glenn Close';
5、id for Casablanca(《卡萨布兰卡》电影的id)
What is the id of the film 'Casablanca'
select id
from movie
where title='Casablanca';
6、Cast list for Casablanca(《卡萨布兰卡》的演员表)
Obtain the cast list for 'Casablanca'.
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 name from actor where id in(select actorid from casting where movieid=11768);
ps:在不知道《卡萨布兰卡》电影id的前提下,还需要用select in select查询moviedid
select name from actor where id in(select actorid from casting where movieid=(select id from movie where title='Casablanca'));
7、Alien cast list(《异形》演员表)
Obtain the cast list for the film 'Alien'
select name
from actor
where id in(select actorid from casting where movieid=(select id from movie where title='Alien'));
8、Harrison Ford movies(Harrison Ford出演的电影名称)
List the films in which 'Harrison Ford' has appeared
select title from movie
left join casting
on movie.id=casting.movieid
where actorid =(select id from actor where name='Harrison Ford');
9、Harrison Ford as a supporting actor(Harrison Ford是配角的电影名称)
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
left join casting
on movie.id=casting.movieid
where actorid =(select id from actor where name='Harrison Ford')and ord>1;
10、Lead actors in 1962 movies(获取1962年,主演演员的姓名,出演电影名称)
List the films together with the leading star for all 1962 films.
select movie.title,actor.name from movie
left join casting
on movie.id=casting.movieid
left join actor
on casting.actorid=actor.id
where ord=1 and yr=1962;
解答思路:本次考察的是多表连接。
11、Busy years for Rock Hudson(Rock Hudson的大热年)
Which were the busiest years for 'Rock Hudson', 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
left join casting
on casting.movieid=movie.id
left join actor
on casting.actorid=actor.id
where actor.name='Rock Hudson'
group by yr
having count(title)>2;
解题思路:️多表连接;️having+聚合函数。
12、Lead actor in Julie Andrews movies(获取Julie Andrews出演的电影名称以及主演名称)
List the film title and the leading actor for all of the films 'Julie Andrews' played in.
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 movie.title,actor.name
from movie
left join casting
on casting.movieid=movie.id
left join actor
on casting.actorid=actor.id
where movie.id in (select movieid from casting where actorid in(select id from actor where name='Julie Andrews')) and casting.ord=1;
解题思路:子查询
13、Actors with 15 leading roles(按照字母排序获取至少担任过15个主演的演员名称)
Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
select actor.name
from actor
join casting
on actor.id=casting.actorid
where casting.ord=1
group by 1
having count(actor.name)>=15
order by 1;
解题思路:聚合函数和having的运用。
14、List the films released in the year 1978 ordered by the number of actors in the cast, then by title.(获取1978年上映的电影名称,按照演员个数倒序,然后是电影名称顺序)
select movie.title,count(casting.actorid)
from movie
join casting
on movie.id=casting.movieid
where movie.yr=1978
group by 1
order by 2 desc,1;
解题思路:join连接movie和casting两个表
15、List all the people who have worked with 'Art Garfunkel'.(列出所有和Art Garfunkel合作的演员姓名)
select DISTINCT(name) from actor
join casting
on actor.id=casting.actorid
where movieid in (select movieid from casting join actor on actor.id=casting.actorid where name='Art Garfunkel') and name!='Art Garfunkel';
解题思路:️合作的演员姓名要排除掉Art Garfunkel自己本身️获取Art Garfunkel出演的电影id,嵌套select子查询。
SQL练习六--More JOIN operations的更多相关文章
- [SQL]SQL语言入门级教材_跟我学SQL(六)
跟我学SQL:(一)数据查询 且不说你是否正在从事编程方面的工作或者不打算学习SQL,可事实上几乎每一位开发者最终都会遭遇它.你多半还用不着负责创建和维持某个,但你怎么着也该知道以下的一些有关的SQL ...
- mysql sql left right inner join区别及效率比较
一.Join语法概述 join 用于多表中字段之间的联系,语法如下: ... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona table1 ...
- Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式
当前有两个表,sgroup与sgroupuser,两者通过gKey关联,而sgroup表记录的是组,而sgroupuser记录是组中的用户,因此在sgroupuser中不一定有数据.需要使用Left ...
- sql表连接left join,right join,inner join三者之间的区别
sql表连接left join,right join,inner join区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 (以左表数据为基准,不足补为NULL) ...
- 【转】图解SQL的各种连接join
原帖地址:http://www.nowamagic.net/librarys/veda/detail/936 图解SQL的各种连接join 让你对SQL的连接一目了然 在 2011年12月22日 那天 ...
- SQL点滴2—重温sql语句中的join操作
原文:SQL点滴2-重温sql语句中的join操作 1.join语句 Sql join语句用来合并两个或多个表中的记录.ANSI标准SQL语句中有四种JOIN:INNER,OUTER,LEFTER,R ...
- SQL Server中INNER JOIN与子查询IN的性能测试
这个月碰到几个人问我关于"SQL SERVER中INNER JOIN 与 IN两种写法的性能孰优孰劣?"这个问题.其实这个概括起来就是SQL Server中INNER JOIN与子 ...
- SQL语句中 INNER JOIN的用法!
一.SQL语句中 INNER JOIN的用法? 1.INNER JOIN的作用? 可以在两个或者更多的表中获取结果,得出一张新表. [隐式内连接] 表一 car 购物车 表二 user 用户 发现 ...
- SQL UPDATE with INNER JOIN
mysql - SQL UPDATE with INNER JOIN - Stack Overflowhttps://stackoverflow.com/questions/14491042/sql- ...
随机推荐
- stm32F103RCT6的DMA使用经历
DMA可以直接传输数据,减少了CPU的负担,是个很好的功能,但是用的时候难免会一头雾水.这次做个小小的串口收发程序就碰到了许多问题. 之前没有注意,选择了DMA的circular模式,然后奇怪的事 ...
- CoreWCF 1.0 正式发布,支持 .NET Core 和 .NET 5+ 的 WCF
CoreWCF 1.0 正式发布,支持 .NET Core 和 .NET 5+ 的 WCF https://devblogs.microsoft.com/dotnet/corewcf-v1-relea ...
- InnoDB的逻辑存储结构是什么,表空间组成包括哪些?
一.表空间 在InnoDB中我们创建的表还有对应的索引数据都存储在扩展名为.ibd 的文件中,这个文件路径可以先通过查mysql变量datadir来得到,然后进入对应的数据库名目录,会看到很多ibd, ...
- ghostnet论文解析:ghost
创建日期: 2020-03-02 17:02:54 简介: GhostNet是2020CVPR录用的一篇对卷积操作进行改进的论文.文章的核心内容是Ghost模块(Ghost Module),可以用来替 ...
- Linux系统安装Python3环境
点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 本文基于如下Linux系统版本: 1.默认情况下,Lin ...
- 「Python实用秘技08」一行代码解析地址信息
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第8期 ...
- linux系统平均负载高(load average)
系统平均负载高(load average) 问题现象 两个案例都是:系统平均负载高,但cpu,内存,磁盘io都正常 什么是系统平均负载 平均负载是指单位时间内,系统处于可运行状态和不可中断状态的平均进 ...
- 2┃音视频直播系统之浏览器中通过 WebRTC 拍照片加滤镜并保存
一.拍照原理 好多人小时候应该都学过,在几张空白的纸上画同一个物体,并让物体之间稍有一些变化,然后连续快速地翻动这几张纸,它就形成了一个小动画,音视频播放器就是利用这样的原理来播放音视频文件的 播放器 ...
- kvm 虚拟化技术 1.2 之配置网络桥接
实验一:配置网路桥接模式 默认kvm的网络是NAT模式,一般生产环境用桥接模式,把虚拟机改成桥接模式 1.查看是否安装网桥设备 [root@kibana opt]# rpm -qa|grep brid ...
- typora的下载和基本的使用
目录 typora的下载和基本的使用 typora的下载 typora基本的使用 选择自己喜爱的主题 创建标题 进入编程环境 改变文本样式 插入链接 插入图片 有序列表 无序列表 创建表格 单选框 表 ...