自学SQL网题目及相关代码

自学SQL网代码记录

  • Part 1:SELECT 查询
  • Part 2:条件查询 (constraints) (Pt. 1)
  • Part 3:条件查询(constraints) (Pt. 2)
  • Part 4:查询结果Filtering过滤 和 sorting排序
  • Part 5:复习 SELECT 查询
  • Part 6:用JOINs进行多表联合查询
  • Part 7:外连接(OUTER JOINs)
  • Part 8: 关于特殊关键字 NULLs
  • Part 9:在查询中使用表达式
  • Part 10:在查询中进行统计I (Pt. 1)
  • Part 11:在查询中进行统计II (Pt. 2)
  • Part 12:查询执行顺序

最近在学习SQL,虽然之前学习过,但貌似忘得差不多了。。。
首先将自学sql网的题目敲了一遍,重新了解了一下简单的语法。
这篇文章的目的就在于做个记录。


Part 1:SELECT 查询

1. 找到所有电影的名称

SELECT title FROM movies;

2. 找到所有电影的导演

SELECT director FROM movies;

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

SELECT title,director FROM movies;

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

SELECT title,year FROM movies;

5. 找到所有电影的所有信息

SELECT * FROM movies;

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

SELECT title,id,length_minutes FROM movies;

7. 请列出所有电影的ID,名称和出版国(即美国)

SELECT id,title,"美国" as country FROM movies;


Part 2:条件查询 (constraints) (Pt. 1)

1. 找到id为6的电影

SELECT * FROM movies where id=6;

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

SELECT * FROM movies where year between 2000 and 2010;

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

SELECT * FROM movies where year not between 2000 and 2010;

4. 找到头5部电影

SELECT * FROM movies where id <=5;

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

SELECT * FROM movies where year>=2010 and length_minutes<120;

6. 找到99年和09年的电影,只要列出年份和片长看下

SELECT year,length_minutes FROM movies where year=1999 or year=2009;


Part 3:条件查询(constraints) (Pt. 2)

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 not director="John Lasseter";

4. 找到所有电影名为 “WALL-” 开头的电影

SELECT * FROM movies where title like "WALL-%";

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

SELECT * FROM movies where year=1998;

6. 找出所有Pete导演的电影,只要列出电影名,导演名和年份就可以

SELECT title,director,year FROM movies where director like "%Pete%"

7. John Lasseter导演了两个系列,一个Car系列一个Toy Story系列,请帮我列出这John Lasseter导演两个系列千禧年之后(含千禧年)的电影

SELECT * FROM movies 
where director="John Lasseter" and (title like "%Car%" or title like "%Toy Story%") and year>=2000;


Part 4:查询结果Filtering过滤 和 sorting排序

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

SELECT distinct director FROM movies order by director;

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 desc
limit 1 offset 2;

6. 按导演名字母升序,如果导演名相同按年份降序,取前10部电影给我

SELECT * FROM movies
order by director asc,year desc
limit 10;


Part 5:复习 SELECT 查询

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

SELECT * FROM north_american_cities where country="Canada";

2. 列出所有美国United States的城市按纬度从北到南排序(包括所有字段)

SELECT * 
FROM north_american_cities 
where country="United States" 
order by Latitude desc;

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

SELECT * 
FROM north_american_cities
where Longitude<(select Longitude from north_american_cities where city="Chicago")
order by Longitude asc;

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

SELECT * 
FROM north_american_cities
where country="Mexico"
order by population desc
limit 2;

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

SELECT * 
FROM north_american_cities
where country="United States"
order by population desc
limit 2 offset 2;

6. 北美所有城市,请按国家名字母序从A-Z再按人口从多到少排列看下前10位的城市(包括所有字段)

SELECT * FROM north_american_cities order by Country asc,Population desc limit 10;


Part 6:用JOINs进行多表联合查询

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

SELECT Domestic_sales,International_sales 
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 International_sales>Domestic_sales;

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

SELECT * 
FROM movies
inner join Boxoffice on movies.id=Boxoffice.movie_id
order Rating asc;

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

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


Part 7:外连接(OUTER JOINs)

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

SELECT distinct building FROM employees where building is not null;

2. 找到所有办公室和他们的最大容量

SELECT * FROM Buildings;

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

(DISTINCT)
SELECT distinct Building_name,role 
FROM Buildings 
left join employees on Buildings.Building_name=employees.Building;

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

SELECT distinct building_name,Capacity 
FROM Buildings 
left join employees on Buildings.Building_name=employees.Building
where role is not null;


Part 8: 关于特殊关键字 NULLs

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

SELECT name,role 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;


Part 9:在查询中使用表达式

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
FROM movies
inner join boxoffice on movies.id=boxoffice.movie_id
where director="John Lasseter"
order by (Domestic_sales+International_sales)/Length_minutes desc
limit 3;

5. 电影名最长的3部电影和他们的总销量是多少

SELECT title,
length(title) as "length_title",
Domestic_sales+International_sales as "总销量"
FROM movies
inner join boxoffice on movies.id=boxoffice.movie_id
order by length(title) desc
limit 3;


Part 10:在查询中进行统计I (Pt. 1)

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

SELECT name,years_employed FROM employees order by years_employed desc limit 1;

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(*) as "nums"
FROM employees 
where building is not null
group by building
order by "nums" desc;

5. 就职1,3,5,7年的人分别占总人数的百分比率是多少(给出年份和比率"50%" 记为 50)

SELECT Years_employed,
count(*)*100/(select count(*) from employees) as "占比"
FROM employees
where Years_employed in (1,3,5,7)
group by Years_employed;


Part 11:在查询中进行统计II (Pt. 2)

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

SELECT count(*) FROM employees group by role having role="Artist";

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

SELECT role,count(*) FROM employees group by role;

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

SELECT sum(years_employed) FROM employees group by role having role="Engineer";

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

SELECT role,
CASE WHEN building is null THEN "0" ELSE "1" END AS "有无办公室" ,
count(*)
FROM employees
group by role,"有无办公室";

5. 按角色和就职年份统计人数,年份按0-3,3-6,6-9这种阶梯分组,最后按角色+阶梯分组排序

SELECT role,
years_employed/3 as "分组",
count(*) as "人数"
FROM employees
group by role,"分组";


Part 12:查询执行顺序

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

SELECT director,count(*) 
FROM movies 
group by director;

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

SELECT director,
sum(Domestic_sales+International_sales) as "销售总额"
FROM movies
inner join boxoffice on movies.id=boxoffice.movie_id
group by director;

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

SELECT director,
sum(Domestic_sales+International_sales) as "总销量",
count(*) as "电影数量",
sum(Domestic_sales+International_sales)/count(*) as "平均销量"
FROM movies inner join boxoffice on movies.id=boxoffice.movie_id
group by director
having count(*)>1
order by "平均销量" desc
limit 1;

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

SELECT title,
(select max(Domestic_sales+International_sales) from boxoffice)-(Domestic_sales+International_sales) as "销售额差额"
FROM movies
inner join boxoffice on movies.id=boxoffice.movie_id;

你可能感兴趣的:(SQL,SQL语句,SQL初学者,SQL语句提升)