Bootstrap

TASK 06 综合练习

练习一: 各部门工资最高的员工(难度:中等)
创建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 |
±-----------±---------±-------+

答:第一题上来而言就对我感觉比较难。我看看其他人的作业跟着模仿了一下。

// An highlighted block
 USE shop;
  CREATE TABLE employee
  ( Id int NOT NULL auto_increment,
  Name VARCHAR(8) NOT NULL,
  Salary int not null,
  DepartmentId int not null,
  PRIMARY KEY (Id));
  INSERT INTO employee (Name,Salary,Departmentid) VALUES ('Joe',70000,1);
  INSERT INTO employee (Name,Salary,Departmentid) VALUES ('Henry',80000,2);
INSERT INTO employee (Name,Salary,Departmentid) VALUES ('Sam',60000,2);
INSERT INTO employee (Name,Salary,Departmentid) VALUES ('Max',90000,1);
CREATE TABLE Department (
    ID INT NOT NULL AUTO_INCREMENT,
    Name VARCHAR(8) NOT NULL,
    PRIMARY KEY (ID)
);
USE shop;
INSERT INTO Department (Name) VALUES ('IT');
INSERT INTO Department (Name) VALUES('Sales');
SELECT d.name AS Department, e.Name AS employee,e.salary AS salery
FROM Department AS d
JOIN  (SELECT Name,Salary,Departmentid FROM employee as e1
	   WHERE Salary = (SELECT MAX(salary) FROM employee as e2
       WHERE e1.departmentid= e2.departmentid
	   GROUP BY e2.departmentid)) AS E
ON id= departmentid
ORDER BY e.Salary DESC;

练习二: 换座位(难度:中等)
小美是一所中学的信息科技老师,她有一张 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 |
±--------±--------+
注意:
如果学生人数是奇数,则不需要改变最后一个同学的座位。

答:

// An highlighted block
CREATE TABLE seat
(id int not null auto_increment,
student VARCHAR(10) NOT NULL,
PRIMARY KEY (id));
INSERT INTO seat(student) VALUES ('Abbot');
INSERT INTO seat(student) VALUES ('Doris');
INSERT INTO seat(student) VALUES ('Green');
INSERT INTO seat(student) VALUES ('Emerson');
INSERT INTO seat(student) VALUES ('Jeames');
SELECT (CASE WHEN s.id %2=0 then s.id-1
             WHEN s.id %2!=0 and s.id!= c.counts THEN s.id +1
             ELSE s.id END) AS id,
student
FROM seat AS S,(SELECT COUNT(*) AS counts FROM seat) AS C
ORDER BY id;

这道题也是模仿出来的。

练习三: 分数排名(难度:中等)
编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

创建以下score表:

±—±------+
| Id | Score |
±—±------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
±—±------+
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

±------±-----+
| Score | Rank |
±------±-----+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
±------±-----+
答:
这道题我感觉是真得写不出来。没有赶脚。

剩下的题我也是没有啥感觉。对我而言确实有点困难。

;