码迷,mamicode.com
首页 > 数据库 > 详细

【DataBase】XueSQL Training

时间:2021-05-24 03:16:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:build   外连接   需要   电影   class   group   diff   统计   办公室   

地址:

http://xuesql.cn/

SQL 

-- 认识SQL
SELECT title FROM movies
SELECT title,director FROM movies WHERE Id < 5
SELECT count(*) FROM movies
SELECT 1+1

-- 基础查询
SELECT Title FROM movies
SELECT Director FROM movies
SELECT Title, Director FROM movies
SELECT Title, Year FROM movies
SELECT Title, Id, Length_minutes FROM movies

基础条件

-- 条件查询1
SELECT * FROM Movies WHERE Id = 6
SELECT * FROM Movies WHERE Year BETWEEN 2000 AND 2010
SELECT * FROM Movies WHERE Year  NOT BETWEEN 2000 AND 2010
SELECT * FROM Movies LIMIT 5
SELECT * FROM Movies WHERE Year > 2009 AND Length_minutes < 120
-- 条件查询2
SELECT * FROM Movies WHERE Title LIKE "%Toy Story%"
SELECT * FROM Movies WHERE Director = "John Lasseter"
SELECT * FROM Movies WHERE Director <> "John Lasseter"
SELECT * FROM Movies WHERE Title LIKE "WALL-%"
SELECT * FROM Movies WHERE Year = 1998 AND Title = "A Bugs Life"

 复杂条件

-- 条件查询3
SELECT DISTINCT Director FROM Movies ORDER BY Director ASC
SELECT * FROM Movies ORDER BY Year DESC LIMIT 4
SELECT * FROM Movies ORDER BY Title ASC LIMIT 5
SELECT Title FROM Movies WHERE Director = "John Lasseter" ORDER BY Length_minutes DESC LIMIT 1 OFFSET 2
-- 条件查询4
SELECT * FROM north_american_cities WHERE Country = "Canada"
SELECT * FROM north_american_cities WHERE Longitude < (SELECT Longitude FROM north_american_cities WHERE City = "Chicago") ORDER BY Longitude ASC
SELECT * FROM north_american_cities WHERE Country = "Mexico" ORDER BY Population DESC LIMIT 2
SELECT * FROM north_american_cities WHERE Country = "United States" ORDER BY Population DESC LIMIT 2 OFFSET 2

 联表查询

-- 联表查询1
SELECT * FROM Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id
SELECT * FROM Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id WHERE B.International_sales > B.Domestic_sales
SELECT * FROM Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id ORDER BY Rating DESC
SELECT Director, International_sales FROM Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id ORDER BY International_sales DESC LIMIT 1

外连接

-- 【复习】找到所有有雇员的办公室(buildings)名字 ?
SELECT DISTINCT B.Building_name FROM Employees A INNER JOIN Buildings B ON A.Building = B.Building_name

-- 【复习】找到所有办公室里的所有角色(包含没有雇员的),并做唯一输出(DISTINCT)
SELECT 
 DISTINCT A.Building_name,
 B.Role
FROM
 Buildings A
 LEFT JOIN  Employees B ON A.Building_name = B.Building

-- 【难题】找到所有有雇员的办公室(buildings)和对应的容量
-- 先求去重的building
SELECT DISTINCT Building  FROM Employees WHERE Building  IS NOT NULL
-- 然后虚拟表联表即可
SELECT 
 A.Building,
 B.Capacity
FROM 
 (SELECT DISTINCT Building  FROM Employees WHERE Building  IS NOT NULL) A 
 INNER JOIN Buildings B ON A.Building = B.Building_name

NULL处理:

-- 【复习】找到雇员里还没有分配办公室的(列出名字和角色就可以)
SELECT Role, Name FROM Employees WHERE Building IS NULL
-- 【难题】找到还没有雇员的办公室
SELECT Building_name FROM Buildings WHERE Building_name NOT IN(SELECT DISTINCT Building  FROM Employees WHERE Building IS NOT NULL) 

使用表达式:

-- 【计算】列出所有的电影ID,名字和销售总额(以百万美元为单位计算)
SELECT Id, Title, (B.Domestic_sales + B.International_sales) / 1000000 TotalSales FROM Movies A LEFT JOIN Boxoffice B ON A.Id = Movie_id
-- 【计算】列出所有的电影ID,名字和市场指数(Rating的10倍为市场指数)
SELECT Id, Title, B.Rating * 10 FROM Movies A LEFT JOIN Boxoffice B ON A.Id = Movie_id
-- 【计算】列出所有偶数年份的电影,需要电影ID,名字和年份
SELECT Id, Title, Year FROM Movies WHERE Year % 2 = 0
-- 【难题】John Lasseter导演的每部电影每分钟值多少钱,告诉我最高的3个电影名和价值就可以
SELECT 
 A.Title, (B.Domestic_sales + B.International_sales) / A.Length_minutes Value
FROM 
 Movies A 
 LEFT JOIN Boxoffice B ON A.Id = Movie_id
WHERE
 Director = "John Lasseter"
ORDER BY 
 Value DESC 
LIMIT 3

分组查询:

-- 【统计】找出就职年份最高的雇员(列出雇员名字+年份)
SELECT Name, Years_employed  FROM Employees WHERE Years_employed = (SELECT 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(1) FROM Employees WHERE Building IS NOT NULL GROUP BY Building

11:

-- 【统计】统计一下Artist角色的雇员数量 ?
SELECT COUNT(1) FROM Employees WHERE Role = "Artist"

-- 【分组】按角色统计一下每个角色的雇员数量
SELECT COUNT(1), Role FROM Employees GROUP BY Role

-- 【分组】算出Engineer角色的就职年份总计
SELECT SUM(Years_employed) FROM Employees WHERE Role = "Engineer"

-- 【难题】按角色分组算出每个角色按有办公室和没办公室的统计人数(列出角色,数量,有无办公室,注意一个角色如果部分有办公室,部分没有需分开统计)
SELECT 
 Role,
 CASE WHEN Building IS NULL  THEN 0 ELSE 1 END AS have_b,
 COUNT(Name)
FROM Employees
GROUP BY Role, have_b;

 

最后:

--【复习】统计出每一个导演的电影数量(列出导演名字和数量)
SELECT Director, COUNT(1) FROM Movies GROUP BY Director

-- 【复习】统计一下每个导演的销售总额(列出导演名字和销售总额)
SELECT A.Director, SUM(B.Domestic_sales + B.International_sales) FROM Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id GROUP BY A.Director

-- 【难题】按导演分组计算销售总额,求出平均销售额冠军(统计结果过滤掉只有单部电影的导演,列出导演名,总销量,电影数量,平均销量)
SELECT 
 SUM(B.Domestic_sales + B.International_sales) sum_sale,
 A.Director, 
 COUNT(1) count,
 (SUM(B.Domestic_sales + B.International_sales) / COUNT(1)) avg_sale
FROM
 Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id GROUP BY A.Director
HAVING count > 1 
ORDER BY avg_sale DESC 
LIMIT 1

-- 【变态难】找出每部电影和单部电影销售冠军之间的销售差,列出电影名,销售额差额
-- 先求销售冠军
SELECT MAX(Domestic_sales + International_sales) FROM Boxoffice

-- 列处理
SELECT 
 (
 (SELECT MAX(Domestic_sales + International_sales) FROM Boxoffice) - 
 (B.Domestic_sales + B.International_sales)
 ) sale_diff,
 A.Title
FROM  Movies A LEFT JOIN Boxoffice B ON A.Id = B.Movie_id 

 

【DataBase】XueSQL Training

标签:build   外连接   需要   电影   class   group   diff   统计   办公室   

原文地址:https://www.cnblogs.com/mindzone/p/14733188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!