MySQL练习题

条件查询

查询movies表

movies表中的数据

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoFinding Nemo2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110

part1

找到所有电影的名称title

select title 
from movies;

找到所有电影的导演

select director
from movies;

找到所有电影的名称和导演

select title,director
from movies;

找到所有电影的名称和上映年份

select title,year
from movies;

找到所有电影的所有信息

select *
from movies;

找到所有电影的名称,Id和播放时长

select title,id,length_minutes
from movies;

part2

找到id为6的电影

select *
from movies 
where id = 6;

找到在2000-2010年间year上映的电影

select *
from movies 
where year >= 2000 and year <=2010;

找到不是在2000-2010年间year上映的电影

select *
from movies 
where year < 2000 or year > 2010;

找到头5部电影

select *
from movies 
limit 5 ;

找到2010(含)年之后的电影里片长小于两个小时的片子

select  * 
from movies
where year >= 2010 and length_minutes < 120;

part3

找到所有Toy Story系列电影

select *
from movies
where title like '%Toy Story%';

找到所有John Lasseter导演的电影

select *
from movies
where director like '%John Lasseter%';

找到所有不是John Lasseter导演的电影

select *
from movies
where director not like '%John Lasseter%';

找到所有电影名为"WALL-"开头的电影

select *
from movies
where title like 'WALL-%';

有一部98年电影中文名《虫虫危机》请给我找出来

select *
from movies
where year = 1998;

part4

按导演名排重列出所有电影(只显示导演),并按导演名正序排列

select distinct(director)
from movies
order by director

列出按上映年份最新上线的4部电影

select *
from movies
order by year desc
limit 4;

按电影名字母序升序排列,列出前5部电影

select *
from movies
order by title 
limit 5;

按电影名字母序升序排列,列出上一题之后的5部电影

select *
from movies
order by title 
limit 5,5;

如果按片长排列,John Lasseter导演导过片长第3长的电影是哪部,列出名字即可

select title
from movies
where director like 'John Lasseter'
order by length_minutes desc
limit 2,1;

part5

下面在North_american_cities表中查询

CityCountryPopulationLatitudeLongitude
GuadalajaraMexico150080020.659699-103.349609
TorontoCanada279506043.653226-79.383184
HoustonUnited States219591429.760427-95.369803
New YorkUnited States840583740.712784-74.005941
PhiladelphiaUnited States155316539.952584-75.165222
HavanaCuba210614623.05407-82.345189
Mexico CityMexico855550019.432608-99.133208
PhoenixUnited States151336733.448377-112.074037
Los AngelesUnited States388430734.052234-118.243685
Ecatepec de MorelosMexico174200019.601841-99.050674
MontrealCanada171776745.501689-73.567256
ChicagoUnited States271878241.878114-87.629798

提示:在这个数据表中,你需要熟悉一下latitudes(纬度)和 longitudes(经度)的概念, latitudes在赤道以北是正数,以南是负数;longitudes在子午线东部是正数,以西是负数, 在查询中需要注意 经纬度和东西南北方向的对应关系。

列出所有加拿大人的Canadian信息(包括所有字段)

select * 
from north_american_cities
where country like 'Canada';

列出所有在Chicago西部的城市,从西到东排序(包括所有字段)

select *
from north_american_cities 
where longitude < (
select Longitude
from north_american_cities
where city like 'Chicago')
order by longitude;

用人口数population排序,列出墨西哥Mexico最大的2个城市(包括所有字段)

select *
from north_american_cities
where country like 'Mexico'
order by population desc
limit 2;

列出美国United States人口3-4位的两个城市和他们的人口(包括所有字段)

select *
from north_american_cities
where country like 'United States'
order by population desc
limit 2,2;

多表查询

part1(内连接)

下面用到Movies和Boxoffice表查询

Movies表

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoFinding Nemo2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110

Boxoffice表

Movie_idRatingDomestic_salesInternational_sales
58.2380843261555900000
147.4268492764475066843
88206445654417277164
126.4191452396368400000
37.9245852179239163000
68261441092370001000
98.5223808164297503696
118.4415004880648167031
18.3191796233170162503
77.2244082982217900167
108.3293004164438338580
48.1289916256272900000
27.2162798565200600000
137.2237283207301700000

找到所有电影的国内Domestic_sales和国际销售额

select *
from movies
inner join Boxoffice
on id = movie_id;

找到所有国际销售额比国内销售大的电影

SELECT *
from movies
inner join Boxoffice
on id = movie_id
where International_sales > Domestic_sales;

找出所有电影按市场占有率rating倒序排列

SELECT *
from movies
inner join Boxoffice
on id = movie_id
order by rating desc;

每部电影按国际销售额比较,排名最靠前的导演是谁,国际销量多少

SELECT director,International_sales
from movies
inner join Boxoffice
on id = movie_id
order by International_sales desc
limit 1;

part2(外连接)

下面用两个雇员表 Employees 和 办公室表 Buildings.

Employees表

RoleNameBuildingYears_employed
EngineerBecky A.1e4
EngineerDan B.1e2
EngineerSharon F.1e6
EngineerDan M.1e4
EngineerMalcom S.1e1
ArtistTylar S.2w2
ArtistSherman D.2w8
ArtistJakob J.2w6
ArtistLillia A.2w7
ArtistBrandon J.2w7
ManagerScott K.1e9
ManagerShirlee M.1e3
ManagerDaria O.2w6
EngineerYancy I.null0
ArtistOliver P.null0

Buildings表

Building_nameCapacity
1e24
1w32
2e16
2w20

找到所有有雇员的办公室(buildings)名字

select distinct(building)
from employees
left  join buildings
on building = building_name
where building is not null;

找到所有办公室里的所有角色(包含没有雇员的),并做唯一输出(DISTINCT)

select distinct(building_name),role
from buildings
left join employees
on building = building_name;

找到所有有雇员的办公室(buildings)和对应的容量

select distinct(building_name),capacity
from employees
left join buildings
on building = building_name
where building is not null;

找到雇员里还没有分配办公室的(列出名字和角色就可以)

select name,role
from employees
where building is null;

找到还没有雇员的办公室

select building_name
from buildings 
left join employees 
on building = building_name
where building is null

函数

part1

下面用到movies表和boxoffice表

movies表

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoFinding Nemo2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110

boxoffice表

Movie_idRatingDomestic_salesInternational_sales
58.2380843261555900000
147.4268492764475066843
88206445654417277164
126.4191452396368400000
37.9245852179239163000
68261441092370001000
98.5223808164297503696
118.4415004880648167031
18.3191796233170162503
77.2244082982217900167
108.3293004164438338580
48.1289916256272900000
27.2162798565200600000
137.2237283207301700000

列出所有的电影ID,名字和销售总额(以百万美元为单位计算)

select id,title,(domestic_sales+international_sales)/1000000 as sum
from movies
left join boxoffice
on id = movie_id

列出所有的电影ID,名字和市场指数(Rating的10倍为市场指数)

select id,title,rating*10
from movies
left join boxoffice
on id = movie_id

列出所有偶数年份的电影,需要电影ID,名字和年份

select id,title,year
from movies
where year%2 = 0

John Lasseter导演的每部电影每分钟值多少钱,告诉我最高的3个电影名和价值就可以

select title,
(domestic_sales+international_sales)/Length_minutes value
from movies
left join boxoffice
on id = movie_id
where director like 'John Lasseter'
order by value desc
limit 3;

part2

下面练习基于Employees表

RoleNameBuildingYears_employed
EngineerBecky A.1e4
EngineerDan B.1e2
EngineerSharon F.1e6
EngineerDan M.1e4
EngineerMalcom S.1e1
ArtistTylar S.2w2
ArtistSherman D.2w8
ArtistJakob J.2w6
ArtistLillia A.2w7
ArtistBrandon J.2w7
ManagerScott K.1e9
ManagerShirlee M.1e3
ManagerDaria O.2w6
EngineerYancy I.null0
ArtistOliver P.null0

找出就职年份最高的雇员(列出雇员名字+年份)

SELECT name,max(years_employed)
FROM employees;

按角色(Role)统计一下每个角色的平均就职年份

SELECT role,avg(years_employed)
FROM employees
GROUP BY role;

按办公室名字总计一下就职年份总和

SELECT building,sum(years_employed)
FROM employees
GROUP BY building;

每栋办公室按人数排名,不要统计无办公室的雇员

SELECT building,COUNT(name) count
FROM employees
WHERE building IS NOT null
GROUP BY building
ORDER BY count desc;

part3

下面练习基于Employees表

RoleNameBuildingYears_employed
EngineerBecky A.1e4
EngineerDan B.1e2
EngineerSharon F.1e6
EngineerDan M.1e4
EngineerMalcom S.1e1
ArtistTylar S.2w2
ArtistSherman D.2w8
ArtistJakob J.2w6
ArtistLillia A.2w7
ArtistBrandon J.2w7
ManagerScott K.1e9
ManagerShirlee M.1e3
ManagerDaria O.2w6
EngineerYancy I.null0
ArtistOliver P.null0

统计一下Artist角色的雇员数量

SELECT count(*)
FROM employees
GROUP BY role
HAVING role like 'Artist';

按角色统计一下每个角色的雇员数量

SELECT role,count(*)
FROM employees
GROUP BY role;

算出Engineer角色的就职年份总计

SELECT sum(years_employed)
FROM employees
GROUP BY role
HAVING role like 'Engineer';

按角色分组算出每个角色按有办公室和没办公室的统计人数(列出角色,数量,有无办公室,注意一个角色如果部分有办公室,部分没有需分开统计)

SELECT COUNT(*),
role,
CASE WHEN building IS null THEN 0 ELSE 1 END as rn
FROM employees
GROUP BY role,rn
ORDER BY role,rn;

part4

这是 SELECT 查询的最后一部分,基于movies表和boxoffice表查询

movies表

IdTitleDirectorYearLength_minutes
1Toy StoryJohn Lasseter199581
2A Bug’s LifeJohn Lasseter199895
3Toy Story 2John Lasseter199993
4Monsters, Inc.Pete Docter200192
5Finding NemoFinding Nemo2003107
6The IncrediblesBrad Bird2004116
7CarsJohn Lasseter2006117
8RatatouilleBrad Bird2007115
9WALL-EAndrew Stanton2008104
10UpPete Docter2009101
11Toy Story 3Lee Unkrich2010103
12Cars 2John Lasseter2011120
13BraveBrenda Chapman2012102
14Monsters UniversityDan Scanlon2013110

boxoffice表

Movie_idRatingDomestic_salesInternational_sales
58.2380843261555900000
147.4268492764475066843
88206445654417277164
126.4191452396368400000
37.9245852179239163000
68261441092370001000
98.5223808164297503696
118.4415004880648167031
18.3191796233170162503
77.2244082982217900167
108.3293004164438338580
48.1289916256272900000
27.2162798565200600000
137.2237283207301700000

统计出每一个导演的电影数量(列出导演名字和数量)

SELECT director,count(title)
FROM movies
GROUP BY director;

统计一下每个导演的销售总额(列出导演名字和销售总额)

SELECT director,sum(Domestic_sales+International_sales) as sum
FROM movies
LEFT JOIN boxoffice
on id = movie_id
GROUP BY director;

按导演分组计算销售总额,求出平均销售额冠军(统计结果过滤掉只有单部电影的导演,列出导演名,总销量,电影数量,平均销量)

SELECT director,
sum(Domestic_sales+International_sales) as sum,
count(title),
avg(Domestic_sales+International_sales) as avg
FROM movies
LEFT JOIN boxoffice
on id = movie_id
GROUP BY director
HAVING COUNT(title) > 1
ORDER BY avg DESC
LIMIT 1;

找出每部电影和单部电影销售冠军之间的销售差,列出电影名,销售额差额

WITH topmovie AS (
select movie_id,Domestic_sales+International_sales as topsale
  FROM boxoffice 
  ORDER BY topsale DESC
  limit 1
),
moviesales AS (
select m.title,b.Domestic_sales+b.International_sales as totalsales
  FROM movies m
  LEFT JOIN boxoffice b
  on id = movie_id
)
SELECT  ms.title,tm.topsale-ms.totalsales
FROM moviesales ms
JOIN topmovie tm;
  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 查询练习(50题) MySQL 查询练习(50题) I 1 -- 建表 1 1.1 -- 学生表 1 1.2 -- 课程表 1 1.3 -- 教师表 1 1.4 -- 成绩表 2 1.5 -- 插入学生表测试数据 2 1.6 -- 课程表测试数据 2 1.7 -- 教师表测试数据 2 1.8 -- 成绩表测试数据 2 2 -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 3 3 -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 4 4 -- 3、查询平均成绩大于等于85分的同学的学生编号和学生姓名和平均成绩 4 5 -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩-- (包括有成绩的和无成绩的) 4 6 -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 5 7 -- 6、查询"李"姓老师的数量 5 8 -- 7、查询学过"张三"老师授课的同学的信息 5 9 -- 8、查询没学过"张三"老师授课的同学的信息 5 10 -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 6 11 -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 6 12 -- 11、查询没有学全所有课程的同学的信息 6 13 -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 6 14 -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 7 15 -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 7 16 -- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 7 17 -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息 7 18 -- 17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 8 19 -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 8 20 -- 19、按各科成绩进行排序,并显示排名(实现不完全) 9 21 -- 20、查询学生的总成绩并进行排名 10 22 -- 21、查询不同老师所教不同课程平均分从高到低显示 10 23 -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 10 24 -- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 11 25 -- 24、查询学生平均成绩及其名次 11 26 -- 25、查询各科成绩前三名的记录 12 27 -- 26、查询每门课程被选修的学生数 12 28 -- 27、查询出只有两门课程的全部学生的学号和姓名 12 29 -- 28、查询男生、女生人数 12 30 -- 29、查询名字中含有"风"字的学生信息 12 31 -- 30、查询同名同性学生名单,并统计同名人数 12 32 -- 31、查询1990年出生的学生名单 13 33 -- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 13 34 -- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 13 35 -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 13 36 -- 35、查询所有学生的课程及分数情况; 13 37 -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 14 38 -- 37、查询不及格的课程 14 39 --38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 14 40 -- 39、求每门课程的学生人数 14 41 -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 14 42 -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 15 43 -- 42、查询每门功成绩最好的前两名 15 44 -- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列, 15 45 -- 44、检索至少选修两门课程的学生学号 15 46 -- 45、查询选修了全部课程的学生信息 15 47 -- 46、查询各学生的年龄 16 48 -- 47、查询本周过生日的学生 16 49 -- 48、查询下周过生日的学生 16 50 -- 49、查询本月过生日的学生 16 51 -- 50、查询下月过生日的学生 16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值