课程地址:GitHub - datawhalechina/wonderful-sql: Follow me,从 0 到 1 掌握 SQL,决胜秋招。
练习一: 各部门工资最高的员工
创建Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
创建Department 表,包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
代码如下:
-- 创建Employee表
CREATE TABLE Employee
(Id CHAR(4) NOT NULL,
Name VARCHAR(30) NOT NULL,
Salary INTEGER,
DepartmentID INTEGER ,
PRIMARY KEY (Id));
INSERT INTO Employee VALUES('1','Joe',70000,1);
INSERT INTO Employee VALUES('2','Henry',80000,2);
INSERT INTO Employee VALUES('3','Sam',60000,2);
INSERT INTO Employee VALUES('4','Max',90000,1);
-- 创建Department表
CREATE TABLE Department
(Id CHAR(4) NOT NULL,
Name VARCHAR(30) NOT NULL,
PRIMARY KEY (Id));
INSERT INTO Department VALUES('1','IT');
INSERT INTO Department VALUES('2','Sales');
-- 找出每个部门工资最高的员工
SELECT p2.Name AS Department,
p1.Name AS Employee,
MAX(p1.Salary) AS Salary
FROM Employee AS p1
LEFT OUTER JOIN
Department AS p2 ON p1.departmentid = p2.id
GROUP BY Department;
执行结果:
[创建Employee表]
[创建Department表]
[找出每个部门工资最高的员工]
练习二: 换座位
小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
其中纵列的id是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?
请创建如下所示seat表:
示例:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
假如数据输入的是上表,则输出结果如下:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。
代码如下:
-- 创建seat表
CREATE TABLE seat
(id INTEGER NOT NULL,
student VARCHAR(20) NOT NULL,
PRIMARY KEY (id));
INSERT INTO seat VALUES(1,'Abbot');
INSERT INTO seat VALUES(2,'Doris');
INSERT INTO seat VALUES(3,'Emerson');
INSERT INTO seat VALUES(4,'Green');
INSERT INTO seat VALUES(5,'Jeames');
-- 换座位
SELECT (CASE WHEN MOD(id,2) = 1 AND id = (SELECT COUNT(*) FROM Seat) THEN id
WHEN MOD(id,2) = 1 THEN id+1
ElSE id-1 END) AS id,student
FROM seat
ORDER BY id
执行结果:
[创建seat表]
[换座位]
练习三: 分数排名
假设在某次期末考试中,二年级四个班的平均成绩分别是 93、93、93、91
。
+-------+-----------+
| class | score_avg |
+-------+-----------+
| 1 | 93 |
| 2 | 93 |
| 3 | 93 |
| 4 | 91 |
+-------+-----------+
目前有如下三种排序结果,请根据查询结果书用 sql写出查询
+-------+-----------+-------+-------+-------+
| class | score_avg | rank1 | rank2 | rank3 |
+-------+-----------+-------+-------+-------+
| 1 | 93 | 1 | 1 | 1 |
| 2 | 93 | 1 | 1 | 2 |
| 3 | 93 | 1 | 1 | 3 |
| 4 | 91 | 4 | 2 | 4 |
+-------+-----------+-------+-------+-------+
创建表代码如下:
-- 创建表
CREATE TABLE score
(class INTEGER NOT NULL,
score_avg INTEGER NOT NULL,
PRIMARY KEY (class));
INSERT INTO score VALUES(1,93);
INSERT INTO score VALUES(2,93);
INSERT INTO score VALUES(3,93);
INSERT INTO score VALUES(4,91);
执行结果:
查询代码如下:
SELECT class,score_avg,
RANK() OVER (ORDER BY score_avg) AS RANK1,
DENSE_RANK() OVER (ORDER BY score_avg) AS RANK2,
ROW_NUMBER() OVER (ORDER BY score_avg) AS RANK3
FROM score;
执行结果: