2023-SQLZOO刷题上(0-7,题目+答案+解析)

2023-SQLZOO刷题上(0-7,题目+答案+解析)

作为一名新手推荐算法工程师,不仅需要做建模相关的工作,还需要处理许多数据方面的问题。
SQLZOO是一个很好的SQL刷题网站,可以快速的巩固和温习一下写SQL的能力。


前言

不忘初心,坚持热爱。——Tusunny


0 SELECT basics

1.顯示德國 Germany 的人口
select population from world where name='Germany';
2.查詢面積為 5,000,000 以上平方公里的國家,對每個國家顯示她的名字和人均國內生產總值(gdp/population)
select name,(gdp/population) as 'gdp/population' from world where area>5000000;
3.顯示“Ireland 愛爾蘭”,“Iceland 冰島”,“Denmark 丹麥”的國家名稱和人口
select name,population from world where name in ('Ireland','Iceland','Denmark');
4.顯示面積為 200,000 及 250,000 之間的國家名稱和該國面積
select name,area from world where area between 200000 and 250000;

1 SELECT name

1.找出以 Y 為開首的國家
select name from world where name like 'Y%';
2.找出以 Y 為結尾的國家
select name from world where name like '%Y'
3.找出所有國家,其名字包括字母x
select name from world where name like '%x%';
4.找出所有國家,其名字以 land 作結尾
select name from world where name like '%land';
5.找出所有國家,其名字以 C 作開始,ia 作結尾
select name from world where name like 'C%ia';
6.找出所有國家,其名字包括字母oo
select name from world where name like '%oo%';
7.找出所有國家,其名字包括三個或以上的a
select name from world where name like '%a%a%a%';
8.找出所有國家,其名字以t作第二個字母
select name from world where name like '_t%';
9.找出所有國家,其名字都有兩個字母 o,被另外兩個字母相隔着
select name from world where name like '%o__o%';
10.找出所有國家,其名字都是 4 個字母的
select name from world where name like '____';
11.顯示所有國家名字,其首都和國家名字是相同的
select name from world where name=capital;
12.顯示所有國家名字,其首都是國家名字加上”City”
# 第一种写法
select name from world where capital like concat(name,'%City')

# 第二种写法
select name from world where capital = concat(name,' City')
13.找出所有首都和其國家名字,而首都要有國家名字中出現
select capital,name from world where capital like concat('%',name,'%');
14.找出所有首都和其國家名字,而首都是國家名字的延伸。
# 你應顯示 Mexico City,因它比其國家名字 Mexico 長
# 你不應顯示 Luxembourg,因它的首都和國家名相是相同的
select name from world where name like '____';
15.顯示國家名字,及其延伸詞,如首都是國家名字的延伸
# 第一种写法
select name,capital 
from world 
where capital like concat(name,'%') and capital != name;

# 第二种写法
select name,capital 
from world 
where capital like concat(name,'%') and capital <> name;

!=是后来才加上的。
<>和!=两者意义相同,在可移植性上前者优于后者
故而sql语句中尽量使用<>来做不等判断

2 SELECT from World

1.一个简单的字段查询语句
select name, continent, population 
from world;
2.顯示具有至少2億人口的國家名稱。 2億是200000000,有八個零
select name 
from world 
where population>200000000;
3.找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值
select name,gdp/population as 'gdp/population' 
from world 
where population>200000000;
4.顯示’South America’南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000)得可得到以百萬為單位人口數
select name,population /1000000 as 'population /1000000 ' 
from world 
where continent = 'South America';
5. 顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口
select name,population 
from world 
where name in ('France','Germany','Italy');
6.顯示包含單詞“United”為名稱的國家
select name from world where name like '%United%';
7.成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口展示大國的名稱,人口和面積。
select name,population,area 
from world 
where area>3000000 or population>250000000;
8.美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。
select name,population,area 
from world 
where (area>3000000 and population<250000000) or (population>250000000 and area<3000000 );
9.除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND 函數來顯示的數值到小數點後兩位,對於南美顯示以百萬計人口,以十億計2位小數GDP
select name,round(population/1000000,2) ,round(gdp/1000000000,2) 
from world 
where continent = 'South America';
10. 顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000。顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。
select name,round(gdp/population,-3) 
from world 
where gdp>1000000000000;
11.对于以N开头的国家,请显示名称 - 但用澳大拉西亚代替大洋洲。
select 
name,
case when continent='oceania' then 'Australasia' else continent end
from world 
where name like 'N%';

五个关键词:case when then else end

12.显示名称和大陆 - 但用欧亚大陆来代替欧洲和亚洲;用美国替代 -代替北美,南美或加勒比海地区的每个国家。展示以A或B开头的国家
select name
	,case when continent in ('Europe','Asia') then 'Eurasia' 
		  when continent in ('North America','South America','Caribbean') then 'America'
		  else continent
		  end as'new_continent'
from world 
where name like 'A%' or name like 'B%';
13.大洋洲替换为澳大拉西亚;欧亚大陆和土耳其的国家替换为欧洲/亚洲; 对于加勒比群岛,以’B’开头的加勒比群岛替换为北美洲,其他加勒比岛屿则替换为南美洲。显示所有国家的名称,原始大陆和新大陆
select name,continent
	,case when continent= 'Oceania' then 'Australasia'
	​      when continent in ('Eurasia','Turkey') then 'Europe/Asia'
	​	  when continent ='Caribbean' then 
			  (case when name like 'B%' then 'North America' else 'South America' end)else continent
		  end as 'new_continent'
from world;

3 SELECT from Nobel

1.顯示1950年諾貝爾獎的獎項資料
select * from nobel where yr=1950;
2.顯示誰贏得了1962年文學獎(Literature)
select * from nobel where yr=1950;
3.顯示“愛因斯坦”(‘Albert Einstein’) 的獲獎年份和獎項
select yr,subject from nobel where winner='Albert Einstein';
4.顯示2000年及以後的和平獎(‘Peace’)得獎者
select winner from nobel where yr >= 2000 and subject='Peace';
5. 顯示1980年至1989年(包含首尾)的文學獎(Literature)獲獎者所有細節(年,主題,獲獎者)
# 第一种写法
select * from nobel where yr between 1980 and 1989 and subject = 'Literature';

# 第二种写法
select * from nobel where yr>=1980 and yr<=1989 and subject = 'Literature';
6.顯示總統獲勝者的所有細節
-- 西奧多•羅斯福 Theodore Roosevelt
-- 伍德羅•威爾遜 Woodrow Wilson
-- 吉米•卡特 Jimmy Carter
select * from nobel where winner in ('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter');
7.顯示名字為John 的得獎者。 (注意:外國人名字(First name)在前,姓氏(Last name)在後)
select winner from nobel where winner like 'john%';
8.顯示1980年物理學(physics)獲獎者,及1984年化學獎(chemistry)獲得者
select * from nobel where (yr=1980 and subject='physics') or (yr=1984 and subject='chemistry');
9.查看1980年獲獎者,但不包括化學獎(chemistry)和醫學獎(medicine)
select * from nobel where yr=1980 and subject not in ('chemistry','medicine');
10. 顯示早期的醫學獎(Medicine)得獎者(1910之前,不包括1910),及近年文學獎(Literature)得獎者(2004年以後,包括2004年)
select * from nobel where (yr<1910 and subject='medicine') or (yr>=2004 and subject='literature');
11.Find all details of the prize won by PETER GRÜNBERG
select * from nobel where winner='PETER GRÜNBERG';
12.查找尤金•奧尼爾EUGENE O’NEILL得獎的所有細節
# 第一种写法
# 知识点:当名字中间带'的时候,引用的时候需要变成''
select * from nobel where winner='EUGENE O''NEILL';

# 第二种写法
# 知识点:这里考察了转义符'\'的使用
select * from nobel where winner='EUGENE O\'NEILL';
13.列出爵士的獲獎者、年份、獎頁(爵士的名字以Sir開始)。先顯示最新獲獎者,然後同年再按名稱順序排列
select winner,yr,subject 
from nobel
where winner like 'Sir%'
order by yr desc ,winner asc;

知识点:order by desc降序,asc升序,order by 的默认情况下是asc升序;

14.Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
select winner,subject from nobel
where yr=1984
order by 
case when subject in ('Chemistry','Physics') then 1 else 0 end asc,
subject,winner

知识点:条件表达式 subject in (‘Chemistry’,‘Physics’)

in()条件是一知个Boolean表达式,只有两种取值 true 或者 false,其中满足条件是true ,不满足条件是 false。又因为0会显示在1的前面,所以如果subject不在(‘Physics’,‘Chemistry’)里面,结果就是0,则会显示在前面;如果subject在Physics 和 Chemistry, 结果就是1,则会显示在最后。case when then else end用来分类,将取值 true 转换成1或者取值 false 转换成0。asc 则是将转换好的0,1按照正常的升序排序。

逻辑知识点: 在使用了order by case when subject in (‘Physics’,'Chemistry‘) then 1 else 0 end asc 这个筛选条件之后,是否还有必要order by subject?
答案:有必要,按照order by 后面承接的排序指标的顺序来说,第一个筛选条件会帮我们将subject in (‘Chemistry’,‘Physics’) 压在排序的最后,而后面的order by subject是在前者的基础上进行排序。
在这里插入图片描述

4 SELECT within SELECT

1.列出每個國家的名字name,當中人口population 是高於俄羅斯’Russia’的人口
select name from world where population>(select population from world where name='Russia');
2.列出歐州每國家的人均GDP,當中人均GDP要高於英國’United Kingdom’的數值
select name from world 
where continent='Europe'
and gdp/population>(select gdp/population from world where name='United Kingdom');
3.在阿根廷Argentina及 澳大利亞Australia所在的洲份中,列出當中的國家名字name 及洲分continent。按國字名字順序排序
select name,continent from world
where continent in (select continent from world where name in ('Argrntina','Australia'))
order by name asc;
4.哪一個國家的人口比加拿大Canada的多,但比波蘭Poland的少?列出國家名字name和人口population
select name,population from world 
where population>(select population from world where name='Canada') 
and population<(select population from world where name='Poland');
5. Germany德國(人口8000萬),在Europe歐洲國家的人口最多。Austria奧地利(人口850萬)擁有德國總人口的11%。顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示0。
# 有误写法,先求出小数位数值,乘以100得到百分位数值,再保留小数位数。
# 缺点是这样操作int数值后面会出现大量的0,就和题目要求的格式不一样了。
select 
	name,
	concat(round((population/(select population from world 
			where name='Germany'))*100,0),'%') as population
from world where continent='Europe';

# 正确写法,先求出小数位数值,保留小数位数,最后乘以100得到百分位数值。
select
	name,
	concat(cast(round(population/(select population from world
		    where name = 'Germany'),2)*100 as int),'%') as percentage
from world where continent = 'Europe';

对比一下第一种写法和第二种写法的区别
在这里插入图片描述
解析:
将Int,decimal 转为varchar经常用 concat函数,比如concat(8,’0′) 得到字符串 ’80′
将varchar 转为Int 用 cast(a as signed)。其中a为varchar类型的字符串,signed为将要转换的数据类型

SQLZOO的补充知识点

我們可以用ALL 這個詞對一個列表進行>=或>或<或<=充當比較。例如,你可以用此查詢找到世界上最大的國家(以人口計算):

select name from world 
where population>all(select population from world where popualtion>0);

需要注意的是在子查詢的条件中使用 population>0,因为有些国家的人口是沒有填入,存在 null值。

6.哪些國家的GDP比Europe歐洲的全部所有单独的国家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)
# 第一种写法
select name from world 
where gdp > (select max(gdp) from world where continent='Europe' and gdp is not null)

select name from world 
where gdp > (select max(gdp) from world where continent='Europe' and gdp gdp>0)

# 第二种写法
select name from world 
where gdp > all(select gdp from world where continent='Europe' and gdp is not null)

select name from world 
where gdp > all(select gdp from world where continent='Europe' and gdp>0)
7.在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的
# 第一种写法
select continent,name,area from world a
where area >= all(
    select area from world b
    where a.continent = b.continent and area>0);

# 第二种写法
select continent,name,area from world a
where area = (
    select area from world b
    where a.continent = b.continent and area>0
    order by area desc limit 1
    );
    
# 第三种写法
select continent,name,area from world a
where area = (
    select max(area) from world b
    where a.continent = b.continent and area>0
    ); 

这里使用到了select子查询,
首先是命名区分,示例中外部查询的表格命名为了a,子查询的表格命名为了b;
再者是为什么要在子查询里面加上where a.continent = b.continent这一句,这相当于每次进行查询的时候是在相同的一小段数据里面做子查询得到结果,再返回结果。

8.列出洲份名稱,和每個洲份中國家名字按字母順序是排首位的國家名。(即每洲只有列一國)
# 第一种写法
# 按字母顺序列出每个洲和国家名称是从小到大的排序,所以这里是<=
select continent,name from world a
where name <= all(
    select name from world b
    where a.continent = b.continent);

# 第二种写法
select continent,name from world a
where name = (
    select name from world b
    where a.continent = b.continent
    order by name asc limit 1);

# 第三种写法
select continent,name from world a
where name= (
    select min(name) from world b
    where a.continent = b.continent and area>0
    ); 

字母可以进行大小比较,越小则说明越接近a(A),越大则说明越接近z(Z)

9.找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字name,continent 洲份和population人口
select name,continent,population from world a
where  25000000 >= all( 
    	select population from world b
        where a.continent = b.continent and population>0);
10. 有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent
select name,continent from world a
where population>all(
			select population*3 from world b
			where a.continent=b.continent and b.population>0
			and a.name<>b.name);

易错点:不记得限制y.name<>x.name,将本身排除在外,而这主要会影响比较时候的一一对应顺序,如果不加上限制条件y.name<>x.name,那么对比的国家永远都是相同的,即 population>population*3这个条件将永远不会成立

5 SUM and COUNT

1.展示世界的總人口
select sum(population) from world;

注意1:只使用聚合函数不使用group by时是对整张表作聚合运算
注意2:注意聚合函数都会忽略列中的null值,但是count(*)也就是统计全部数据的行数时,不会忽略null值

2.列出所有的洲份, 每個只有一次
# 第一种写法
select distinct(continent) from world;

# 第二种写法
select continent from world group by continent;
3.找出非洲(Africa)的GDP總和
select sum(gdp) from world where continent='Africa';
4.有多少個國家具有至少百萬(1000000)的面積
# 第一种写法
select count(name) as num from world where area>1000000;

# 第二种写法
select count(1) as num from world where area>1000000;
5. (‘France’,‘Germany’,‘Spain’)(“法國”,“德國”,“西班牙”)的總人口是多少?
select sum(population) from world where name in ('France','Germany','Spain');
6.對於每一個洲份,顯示洲份和國家的數量
# 第一种写法
select continent,count(name) as countries_num from world group by continent;

# 第二种写法
select continent,count(1) as countries_num from world group by continent;
7. 對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目
# 第一种写法
select continent,count(name) as countries_num from world where population>10000000 group by continent;

# 第二种写法
select continent,count(1) as countries_num from world where population>10000000 group by continent;
8.列出有至少100百萬(1億)(100,000,000)人口的洲份
select continent from world 
group by continent 
having sum(population)>=100000000;

having子查询
having 和where 一样用于条件筛选,两者的操作符一致,但不同的是where是在指定分组前对数据进行筛选过滤,而having可以对分组后的数据进行筛选过滤。
需要严格按照语法的顺序 where ->group by -> having。

6 JOIN

1.第一個例子列出球員姓氏為’Bender’的入球數據。 * 表示列出表格的全部欄位,簡化了寫matchid, teamid, player, gtime語句
select matchid,player from goal where teamid='GER';
2.由以上查詢,你可見Lars Bender’s 於賽事 1012入球。.現在我們想知道此賽事的對賽隊伍是哪一隊。留意在 goal 表格中的欄位 matchid ,是對應表格game的欄位id。我們可以在表格 game中找出賽事1012的資料。
select id,stadium,team1,team2
from game left join goal on id=matchid
where id = '1012' and player = 'Lars Bender';
3.修改它來顯示每一個德國入球的球員名,隊伍名,場館和日期。
select player,teamid,stadium,mdate
from game left jion goal on id=matchid
where teamid = 'GER';
4.列出球員名字叫Mario (player LIKE ‘Mario%’)有入球的 隊伍1 team1, 隊伍2 team2 和 球員名 player
select team1,team2,player 
from game join goal on matchid=id
where player like 'Mario%';
5. 表格eteam 貯存了每一國家隊的資料,包括教練。你可以使用語句 goal JOIN eteam on teamid=id來合拼 JOIN 表格goal 到 表格eteam
select player,teamid,coach,gtime 
from goal join eteam on teamid=id
where gtime<=10;
6.列出’Fernando Santos’作為隊伍1 team1 的教練的賽事日期,和隊伍名
select mdate,teamname 
from game join eteam on (team1 = eteam.id)
where coach = 'Fernando Santos';
7.列出場館 'National Stadium, Warsaw’的入球球員
select player 
from game left join goal on game.id=goal.matchid 
where stadium='National Stadium, Warsaw';
8.修改它,只列出全部賽事,射入德國龍門的球員名字。
# 第一种写法
select distinct(player)
from game left join goal on game.id=goal.matchid
where (team1='GER' or team2='GER') and teamid <> 'GER';

# 第二种写法
select player
from game left join goal on game.id=goal.matchid
where (team1='GER' or team2='GER') and teamid <> 'GER'
group by player;
9.列出隊伍名稱 teamname 和該隊入球總數
select teamname,count(gtime)
from eteam left join goal on eteam.id=goal.teamid
group by teamname;

本题中若使用,count(1)会将链接表后,gtime不会出现null,所以不会将gtime为空的行null也统计进去。

10. 列出場館名和在該場館的入球數字
select stadium,count(gtime)
from game left join goal on game.id=goal.matchid
group by stadium;

本题中若使用,count(1)会将链接表后,gtime会出现null,所以如果使用count(1),会将gtime为空的行null也统计进去。

11.每一場波蘭’POL’有參與的賽事中,列出賽事編號 matchid, 日期date 和入球數字
select matchid,mdate,count(gtime)
from game left join goal on matchid = id
where (team1='POL' OR team2='POL')
group by matchid,mdate;
12.每一場德國’GER’有參與的賽事中,列出賽事編號 matchid, 日期date 和德國的入球數字
select matchid,mdate,count(teamid)
from game join goal on id = matchid
where teamid = 'GER'
group by matchid,mdate;
13.List every match with the goals scored by each team as shown. This will use “CASE WHEN” which has not been explained in any previous exercises.列出每一场比赛,每支球队的进球数如下所示。

在这里插入图片描述

select 
	a.mdate,
	a.team1, 
	sum(case when a.team1=b.teamid then 1 else 0 end)as score1,
	a.team2, 
	sum(case when a.team2=b.teamid then 1 else 0 end) as score2
from game a left join goal b
on a.id=b.matchid
group by a.mdate,a.team1,a.team2

7 More JOIN operations

1.列出1962年首影的電影, [顯示 id, title]
select id,title from movie where yr=1962;
2.電影大國民 ‘Citizen Kane’ 的首影年份。
select yr from movie where title='Citizen Kane';
3.列出全部Star Trek星空奇遇記系列的電影,包括id, title 和 yr(此系統電影都以Star Trek為電影名稱的開首)。按年份順序排列。
select id,title,yr from movie 
where title like 'Star Trek%' 
order by yr asc;
4.id是 11768, 11955, 21191 的電影是什麼名稱?
select title from movie where id in (11768,11955,21191);
5. 女演員’Glenn Close’的編號 id是什麼?
select id from actor where name='Glenn Close';
6.電影北非諜影’Casablanca’ 的編號 id是什麼?
select id from movie where title='Casablanca';
7.列出電影北非諜影 'Casablanca’的演員名單。
select name 
from actor where id in (select actorid 
from casting where movieid=(select id from movie where title='Casablanca'));
8.顯示電影異型’Alien’ 的演員清單。
# 第一种写法
select name 
from actor 
join casting on id = actorid 
where movieid = (select id from movie where title = 'Alien');

# 第二种写法
select actor.name 
from casting join movie 
on (casting.movieid=movie.id)
join actor 
on (actor.id=casting.actorid)
where title='Alien';

# 第三种写法
select actor.name
from actor join casting
on (actor.id=casting.actorid)
join movie
on (movie.id=casting.actorid)
where movie.title='Alien';
9.列出演員夏里遜福 ‘Harrison Ford’ 曾演出的電影。
# 第一种写法
select movie.title
from movie join casting 
on (movie.id=casting.movieid)
join actor
on (actor.id=casting.actorid)
where actor.name='Harrison Ford';

# 第二种写法
select movie.title
from actor join casting
on (actor.id=casting.actorid)
join movie
on (movie.id=casting.movieid)
where actor.name='Harrison Ford';
10. 列出演員夏里遜福 ‘Harrison Ford’ 曾演出的電影,但他不是第1主角。
select actor.name
from actor join casting 
on (actor.id=casting.actorid)
join movie
on (movie.id = casting.movieid)
where movie.title = 'Alien' ;
11.列出1962年首影的電影及它的第1主角。
select movie.title,actor.name
from movie join casting
on (movie.id = casting.movieid)
join actor
on (casting.actorid = actor.id)
where movie.yr='1962' and casting.ord=1
12.尊·特拉華達’John Travolta’最忙是哪一年? 顯示年份和該年的電影數目。

movie電影(id编號,title電影名稱,yr首影年份,director導演,budget製作毒,gross票房收入)
casting,角色(movieid電影编號,actorid演員编號,ord角色次序)
actor演員(id编號,name姓名)

# 第一步,分组计算每年John Travolta参演电影数量
select yr,count(title) as title_num
from movie join casting on movie.id = casting.movieid
		   join actor on casting.actorid=actor.id
where name='John Travolta'
group by yr

# 第二步,在上一步的基础上找到单年John Travolta参演电影数量的最大值(这里是2)
select max(title_num) from
(select yr,count(title) as title_num 
from movie join casting on movie.id=movieid
           join actor on actorid=actor.id
where name='John Travolta'
group by yr) as temp1

# 第三步,在group by分组后使用having进行子查询(筛选)操作
select yr,count(title)
from movie join casting on movie.id = casting.movieid
		   join actor on casting.actorid=actor.id
where name='John Travolta'
group by yr
having count(title)=(
select max(title_num) from
(select yr,count(title) as title_num 
from movie join casting on movie.id=movieid
           join actor on actorid=actor.id
where name='John Travolta'
group by yr) as temp1
)

#第三步容易错,where和having一样子句中不能用字段的别名,只能用字段最原始的名字(或加了聚合函数的原始字段),下面是错误写法
select yr,count(title) as num
from movie join casting on movie.id = casting.movieid
		   join actor on casting.actorid=actor.id
where name='John Travolta'
group by yr
having num=(
select max(title_num) from
(select yr,count(title) as title_num 
from movie join casting on movie.id=movieid
           join actor on actorid=actor.id
where name='John Travolta'
group by yr) as temp1
)

第一步:
在这里插入图片描述
第二步:
在这里插入图片描述
第三步:
在这里插入图片描述

13.列出演員茱莉·安德絲’Julie Andrews’曾參與的電影名稱及其第1主角。
# 第一步,找出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱
select title
from movie join casting on movie.id = casting.movieid
		   join actor on casting.actorid=actor.id
where name='Julie Andrews'

# 第二步,筛选出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱及其第1主角
select title,name
from movie join casting on movie.id = casting.movieid
		   join actor on casting.actorid=actor.id
where title in (select title
from movie join casting on movie.id = casting.movieid
		   join actor on casting.actorid=actor.id
where name='Julie Andrews')
and ord=1

第一步:
在这里插入图片描述
第二步:
在这里插入图片描述

14.列出按字母順序,列出哪一演員曾作30次第1主角。
# 第一种写法
select name
from movie join casting on movie.id = casting.movieid
           join actor on casting.actorid=actor.id
where ord=1
group by name
having count(title)>= 30
order by name

# 第二种写法(更优)
select name 
from casting join actor on actorid=actor.id
where ord = 1 
group by name
having  count(movieid)>= 30
order by name
15.列出1978年首影的電影名稱及角色數目,按此數目由多至少排列。
select title,count(actorid) as actor_nums
from movie join casting on movie.id = casting.movieid
where yr=1978
group by title
order by count(actorid) desc,title
16.列出曾與演員亞特·葛芬柯’Art Garfunkel’合作過的演員姓名。
select name 
from movie join casting on movie.id = casting.movieid
       	   join actor on casting.actorid=actor.id
where movie.id in (
	select distinct movie.id 
	from movie join casting on movie.id =casting.movieid 
			   join actor on casting.actorid=actor.id 
	where name = 'Art Garfunkel'
	) 
and name!='Art Garfunkel'

-- 这道题还可以用with试图来写,因为这里有重复表的使用,这样写查询效能更高。
-- 需要注意的是三张表中相同的字段,对于这些字段,join后是要重命名的,有点麻烦,所以我懒得写了。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值