SQL自学网习题答案
作者:互联网
附学习网址:自学SQL网(教程 视频 练习全套)
第一节:
1【初体验】这是第一题,请你先将左侧的输入框里的内容清空,然后请输入下面的SQL,您将看到所有电影标题:
答案:SELECT * FROM movies;
2【初体验】请输入如下SQL你将看到4条电影(切记先清空数据框且出错要耐心比对):
答案:SELECT title,director FROM movies WHERE Id < 5
3【初体验】输入如下SQL你将看到电影总条数:
答案:SELECT count(*) FROM movies
4【初体验】SQL可以直接做计算,下面的SQL计算1+1的和,请输入:
答案:SELECT 1+1
第二节:
1【简单查询】找到所有电影的名称
答案:SELECT title FROM movies
2【简单查询】找到所有电影的导演
答案:SELECT Director FROM movies
3【简单查询】找到所有电影的名称和导演
答案:SELECT Director,title FROM movies
4【简单查询】找到所有电影的名称和上映年份
答案:SELECT title,Year FROM movies
5【简单查询】找到所有电影的所有信息
答案:SELECT * FROM movies
6【简单查询】找到所有电影的名称,Id和播放时长
答案:SELECT title,Id,Length_minutes FROM movies
第三节:
1【简单条件】找到id为6的电影
答案:SELECT * FROM movies where Id=6
2【简单条件】找到在2000-2010年间year上映的电影
答案:SELECT * FROM movies where Year>=2000 and Year<=2010
3【简单条件】找到不是在2000-2010年间year上映的电影
答案:SELECT * FROM movies where Year<2000 or Year >2010
4【简单条件】找到头5部电影
答案:SELECT * FROM movies where Id<=5
5【简单条件】找到2010(含)年之后的电影里片长小于两个小时的片子
答案:SELECT * FROM movies where Year>=2010 and Length_minutes<120
第四节:
1【复杂条件】找到所有Toy Story系列电影
答案:SELECT * FROM movies where Title LIKE “%Toy Story%”
2【复杂条件】找到所有John Lasseter导演的电影
答案:SELECT * FROM movies where Director =‘John Lasseter’
3【复杂条件】找到所有不是John Lasseter导演的电影
答案:SELECT * FROM movies where Director !=‘John Lasseter’
4【复杂条件】找到所有电影名为 “WALL-” 开头的电影
答案:SELECT * FROM movies where Title LIKE ‘WALL-%’
5【复杂条件】有一部98年电影中文名《虫虫危机》请给我找出来
答案:SELECT * FROM movies where Title = “A Bug’s Life”
第五节:
1【结果排序】按导演名排重列出所有电影(只显示导演),并按导演名正序排列
答案:SELECT DISTINCT Director FROM movies ORDER BY Director ASC
2【结果排序】列出按上映年份最新上线的4部电影
答案:SELECT * FROM movies ORDER BY Year DESC limit 4
3【结果排序】按电影名字母序升序排列,列出前5部电影
答案:SELECT * FROM movies ORDER BY Title ASC limit 5
4【结果排序】按电影名字母序升序排列,列出上一题之后的5部电影
答案:SELECT * FROM movies ORDER BY Title ASC limit 5 OFFSET 5
5【结果排序】如果按片长排列,John Lasseter导演导过片长第3长的电影是哪部,列出名字即可
答案:SELECT Title FROM movies WHERE Director=‘John Lasseter’ ORDER BY Length_minutes ASC limit 1 offset 2
第六节:
1【复习】列出所有加拿大人的Canadian信息(包括所有字段)
答案:SELECT * FROM north_american_cities WHERE Country = ‘Canada’
2【复习】列出所有在Chicago西部的城市,从西到东排序(包括所有字段)
答案:SELECT * FROM north_american_cities WHERE Longitude<-87.629798 ORDER BY Longitude DESC
3【复习】用人口数population排序,列出墨西哥Mexico最大的2个城市(包括所有字段)
答案:SELECT * FROM North_american_cities WHERE Country=‘Mexico’ ORDER BY Population ASC LIMIT 2 OFFSET 1
4【复习】列出美国United States人口3-4位的两个城市和他们的人口(包括所有字段)
答案:SELECT * FROM North_american_cities WHERE country = ‘United States’ ORDER BY Population DESC LIMIT 2 OFFSET 2
第七节:
1【联表】找到所有电影的国内Domestic_sales和国际销售额
答案:SELECT * FROM movies inner join boxoffice on movies.id = boxoffice.movie_id
2【联表】找到所有国际销售额比国内销售大的电影
答案:SELECT * FROM movies inner join Boxoffice on Movies.Id = Boxoffice.Movie_id WHERE Domestic_sales<International_sales
3【联表】找出所有电影按市场占有率rating倒序排列 *****
答案:SELECT * FROM movies inner join Boxoffice on Movies.Id = Boxoffice.Movie_id ORDER BY rating DESC
4【联表】每部电影按国际销售额比较,排名最靠前的导演是谁,国际销量多少
答案:SELECT Director,International_sales FROM movies inner join Boxoffice on Movies.Id=Boxoffice.Movie_id ORDER BY International_sales DESC LIMIT 1
第八节:
1【复习】找到所有有雇员的办公室(buildings)名字
答案:SELECT DISTINCT building FROM employees where building is not null
2【复习】找到所有办公室里的所有角色(包含没有雇员的),并做唯一输出(DISTINCT)
答案:SELECT DISTINCT buildings.building_name, employees.Role FROM buildings left join employees on buildings.building_name = employees.building
3【难题】找到所有有雇员的办公室(buildings)和对应的容量
答案:SELECT DISTINCT Buildings.Building_name,Buildings.Capacity FROM Employees inner JOIN Buildings ON Employees.Building=Buildings.Building_name WHERE Building is not null
第九节:
1【复习】找到雇员里还没有分配办公室的(列出名字和角色就可以)
答案:SELECT Role,Name FROM employees WHERE Building is null
2【难题】找到还没有雇员的办公室
答案:SELECT Building_name FROM buildings left join employees on buildings.building_name = employees.building WHERE Role IS NULL
第十节:
1【计算】列出所有的电影ID,名字和销售总额(以百万美元为单位计算)
答案:SELECT Id,Title,(Domestic_sales+International_sales)/1000000 as 销售总额 FROM movies inner join Boxoffice on Movies.Id=Boxoffice.Movie_id
2【计算】列出所有的电影ID,名字和市场指数(Rating的10倍为市场指数)
答案:SELECT Id,Title,rating*10 as 市场指数 FROM movies inner join Boxoffice on Movies.Id=Boxoffice.Movie_id
3【计算】列出所有偶数年份的电影,需要电影ID,名字和年份
答案:SELECT Id,Title,year FROM movies inner join Boxoffice on Movies.Id=Boxoffice.Movie_id WHERE Year%2=0
4【难题】John Lasseter导演的每部电影每分钟值多少钱,告诉我最高的3个电影名和价值就可以
答案:SELECT Title,(Domestic_sales+International_sales)/Length_minutes as price FROM movies inner join Boxoffice on Movies.Id=Boxoffice.Movie_id WHERE Director=‘John Lasseter’ order by price DESC limit 3
第十一节:
1【统计】找出就职年份最高的雇员(列出雇员名字+年份)
答案:SELECT Name,max(Years_employed) FROM employees
2【分组】按角色(Role)统计一下每个角色的平均就职年份
答案:SELECT role,avg(Years_employed) FROM employees group by role
3【分组】按办公室名字总计一下就职年份总和
答案:SELECT Building,sum(Years_employed) FROM employees group by Building
4【难题】每栋办公室按人数排名,不要统计无办公室的雇员
答案:SELECT Building,count(Building)as count FROM employees group by Building order by count DESC limit 2
{SELECT building,count(*) as count FROM employees where building is NOT null group by building
}
第十二节:
1【统计】统计一下Artist角色的雇员数量
答案:SELECT count() FROM employees WHERE Role = ‘Artist’
2【分组】按角色统计一下每个角色的雇员数量
答案:SELECT role,count() FROM employees group by role
3【分组】算出Engineer角色的就职年份总计
答案:SELECT sum(Years_employed) FROM employees where role='Engineer’group by Role
4【难题】按角色分组算出每个角色按有办公室和没办公室的统计人数(列出角色,数量,有无办公室,注意一个角色如果部分有办公室,部分没有需分开统计)
答案:SELECT count(*) as count,Role,building is not null as bn FROM employees group by Role,bn
第十三节:
1【复习】统计出每一个导演的电影数量(列出导演名字和数量)
答案:SELECT DIRECTOr, count() FROM movies group by director
2【复习】统计一下每个导演的销售总额(列出导演名字和销售总额)
答案:SELECT DIRECTOr,sum(Domestic_sales)+sum(International_sales) as 销售总额 FROM movies inner join Boxoffice on Movies.Id = Boxoffice.Movie_id group by Director
3【难题】按导演分组计算销售总额,求出平均销售额冠军(统计结果过滤掉只有单部电影的导演,列出导演名,总销量,电影数量,平均销量)
答案:SELECT sum(Domestic_sales+International_sales) as sum_sale,director,count() as count, sum(Domestic_sales+International_sales)/count() as avg_sale FROM movies left join boxoffice on movies.id = boxoffice.movie_id group by director having count > 1 order by avg_sale desc limit 1
**** SELECT Director, SUM(Domestic_sales)+SUM(International_sales) AS TOTAL,count() AS COUNTS,(SUM(Domestic_sales)+SUM(International_sales))/count(*) as avg FROM MOVIES LEFT join Boxoffice on movies.Id=Boxoffice.movie_id group by director having countS > 1 order by avg DESC LIMIT 1
4【变态难】找出每部电影和单部电影销售冠军之间的销售差,列出电影名,销售额差额
答案:SELECT (SELECT (Domestic_sales+International_sales) as total_sale FROM movies left join boxoffice on movies.id = boxoffice.movie_id order by total_sale desc limit 1) - (Domestic_sales+International_sales) as sale_diff,title FROM movies left join boxoffice on movies.id = boxoffice.movie_id order by sale_diff desc
标签:电影,sales,Boxoffice,movies,答案,SQL,习题,自学,SELECT 来源: https://www.cnblogs.com/txq521/p/16592518.html