MySQL学习笔记(三)

MySQL学习笔记(三)

任务一

各部门工资最高的员工。

创建 Employee 表

表中包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

1、建表

create table Employee(
Id int auto_increment primary key,
Name varchar not null,
Salary int not null,
DepartmentId int not null
);

2、插入数据

insert into Emploee (Name,Salary,DepartmentId) values(Joe,70000,1),
(Henry,80000,2),
(Sam,60000,2),
(Max,90000,1);

创建department表

包含公司所有部门的信息
1、建表

create table Department(
Id int primary key auto_increment,
Name varchar not null
);

2、插入数据

insert into Department(Name) 
values (IT),
	   (Sales);

找出每个部门工资最高的员工

SELECT Department.Name as Department,Emploe.Name as Emploee,max(Emploee.Salary) as Salary 
FROM Emploee,Department where Employee.Id=Department.Id 
GROUP BY Emploee.DepartmentId;

任务二

换座位
小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。
其中纵列的 id 是连续递增的
小美想改变相邻俩学生的座位。
你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

创建seat表

1、建表

create table seat(
id int primary key auto_increment,
student varchar not null);

2、 插入数据

insert into seat(student) 
values
	(Abbot),
	(Doris),
	(Emerson),
	(Green),
	(Jeames);

3、更新表格

UPDATE seat s1
JOIN seat s2
ON (s1.id % 2 = 1 AND s2.id = s1.id+1)
SET s1.student=s2.student,s2.student=s1.student
WHERE s1.id+1 <> null;

任务三

分数排名
编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
1、建立score表

create table score(
Id int auto_increment primary key,
Score float not null);

2、插入数据

insert into score(Id,Score) 
values
	(1,3.50),
	(2,3.65),
	(3,4.00),
	(4,3.85),
	(5,4.00),
	(6,3.65);

3、查询语句

SELECT Score,
CASE 
WHEN @prevRank = Score THEN @curRank 
WHEN @prevRank := Score THEN @curRank := @curRank + 1
END AS Rank
FROM scores,
(SELECT @curRank :=0, @prevRank := NULL) 
ORDER BY Score desc;

任务四

行程和用户
Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。

建立trips表

1、建立表格

CREATE TABLE Trips (
    Id INT NOT NULL PRIMARY KEY,
    Client_Id int NOT NULL,
    Driver_Id int not null,
    City_Id int not null,
    Status ENUM('completed', 'cancelled_by_driver','cancelled_by_client'),
    Request_at date,
    foreign key(Client_Id) references Users(Users_Id),
    foreign key(Driver_Id) references Users(Users_Id)
);

2、插入数据

insert into Trips
 values
(1,1,10,1,'completed','2013-10-01'),
(2,2,11,1,'cancelled_by_driver','2013-10-01'),
(3,3,12,6,'completed','2013-10-01'),
(4,4,13,6,'cancelled_by_client','2013-10-01'),
(5,1,10,1,'completed','2013-10-02'),
(6,2,11,6,'completed','2013-10-02'),
(7,3,12,6,'completed','2013-10-02'),
(8,2,12,12,'completed','2013-10-03'),
(9,3,10,12,'completed','2013-10-03'),
(10,4,13,12,'cancelled_by_driver','2013-10-03');

建立Users表

1、建表

CREATE TABLE Users (
    Users_Id INT NOT NULL PRIMARY KEY,
    Banned varchar(3) NOT NULL,
    Role ENUM('client', 'driver','partner')
);

2、插入数据

insert into Users(Users_Id,Banned,Role) 
values
(1,'No','client'),
(2,'Yes','client'),
(3,'No','client'),
(4,'No','client'),
(10,'No','driver'),
(11,'No','driver'),
(12,'No','driver'),
(13,'No','driver');

写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。

SELECT t1.Request_at,t1.c1/t2.c2 as Cancellation Rate FROM
(SELECTcount(Status) as c1 Request_at FROM Trips WHERE Client_Id in (SELECT Users_id FROM Users WHERE Role=client && Banned=No;) AND Status <> completed GROUP BY Request_at;) t1,
(SELECT count(Status) as c2 Request_at FROM Trips where Client_Id IN (SELECT Users_id FROM Users WHERE Role=client && Banned=No;) GROUP BY Request_at;) t2
WHERE
t1.Request_at=t2.Request_at;

任务五

各部门前3高工资的员工
1、清空表

TRUNCATE TABLE  employee;

2、重新插入数据

insert into Emploee (Name,Salary,DepartmentId) values(Joe,70000,1),
(Henry,80000,2),
(Sam,60000,2),
(Max,90000,1),
(5 ,Janet,69000,1),
(6 ,Randy,85000,1);    

编写一个 SQL 查询,找出每个部门工资前三高的员工。

SELECT 
    d.name AS Department, e.Name AS Employee, Salary
FROM
    Employee e
        JOIN
    Department d ON e.DepartmentId = d.Id
WHERE
    (SELECT 
            COUNT(DISTINCT em.Salary)
        FROM
            Employee em
        WHERE
            em.Salary >= e.Salary
                AND em.DepartmentId = e.DepartmentId) <= 3
GROUP BY Department , Salary DESC;

任务六

分数排名
1、建表

create table score(
Id int auto_increment primary key,
Score float not null);

2、插入数据

insert into score(Score,Rank) 
values
	(4.00,1),
	(4.00,1),
	(3.85,3),
	(3.65,3),
	(3.65,4),
	(3.50,6);

3、查询语句

SELECT Score Rank FROM
(SELECT Score,
@curRank := IF(@prevRank = Score, @curRank, @incRank) AS Rank,
@incRank := @incRank + 1, 
@prevRank := Score
FROM scores,(
SELECT @curRank :=0, @prevRank := NULL, @incRank := 1
) 
ORDER BY Score desc;

你可能感兴趣的:(学习笔记)