DATAWHALE 第六章:决胜秋招
一、练习一:各部门工资最高的员工(难度:中等)
1.创建Employee 表,包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id
create table employee(
employee_id char(4) not null,
employee_name varchar(100) not null,
salary integer not null,
department_id char(4) not null,
primary key(Employee_id)
);
insert into employee values('0001','Joe','70000','0001');
insert into employee values('0002','Henry','80000','0002');
insert into employee values('0003','Sam','60000','0002');
insert into employee values('0004','Max','50000','0001');
2.创建Department 表,包含公司所有部门的信息。
create table department(
department_id char(4) not null,
department_name varchar(100) not null,
primary key(department_id)
);
insert into department values('0001','IT');
insert into department values('0002','Sales');
3.编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
select employee_name, max(salary) as max_salary from employee
group by department_id
练习二: 换座位(难度:中等)
1.小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
(1)创建表格:
create table seat(id int(11) not null,
student varchar(100) not null,
primary key(id));
-- 插入数据
insert into seat(id,student) values('1','ABBot');
insert into seat(id,student) values('2','Doris');
insert into seat(id,student) values('3','Emerson');
insert into seat(id,student) values('4','Green');
insert into seat(id,student) values('5','Jeames');
(2)改变相邻俩学生的座位:
SELECT
(CASE
WHEN MOD(id, 2) != 0 AND counts != id THEN id + 1
WHEN MOD(id, 2) != 0 AND counts = id THEN id
ELSE id - 1
END) AS id,
student
FROM
seat,
(SELECT
COUNT(*) AS counts
FROM
seat) AS seat_counts
ORDER BY id ASC;