连接Mysql
d:
cd D:\mysql-5.7.27-winx64\mysql-5.7.27-winx64\bin
net start mysql
mysql -u root -p
use DataWhaletwo;
CREATE TABLE score(
id int,
score DECIMAL(3,2)
);
INSERT INTO score
VALUES
(1,3.50),
(2,3.65),
(3,4.00),
(4,3.85),
(5,4.00),
(6,3.65);
mysql> select @rank_tmp:=0,@pre_score:=NULL;
SELECT id, score
, CASE
WHEN @pre_score = score THEN @rank_tmp
WHEN @pre_score := score THEN @rank_tmp := @rank_tmp + 1
END AS 'rank'
FROM score, (
SELECT @rank_tmp := 0, @pre_score := NULL
) tmp
ORDER BY score.score DESC;
求出survey_log表中回答率最高的问题,表格的字段有:uid, action, question_id, answer_id, q_num, timestamp。
uid是用户id;action的值为:“show”, “answer”, “skip”;当action是"answer"时,answer_id不为空,相反,当action是"show"和"skip"时为空(null);q_num是问题的数字序号。
写一条sql语句找出回答率最高的问题。
举例:
输入
CREATE TABLE survey_log(
id int PRIMARY KEY,
uid int(11) ,
action varchar(20),
question_id int(11),
answer_id int(11),
q_num int(11),
timestamp varchar(20)
);
INSERT INTO survey_log VALUES
('1', '5', 'show', '285', null, '1', '123'),
('2', '5', 'answer', '285', '124124', '1', '124'),
('3', '5', 'show', '369', null, '2', '125'),
('4', '5', 'skip', '369', null, '2', '126');
SELECT sum(question_id) FROM survey_log WHERE action LIKE 'show';
SELECT SUM(CASE
WHEN action LIKE 'answer' THEN 1
ELSE 0
END)
FROM survey_log;
SELECT SUM(CASE
WHEN action LIKE 'answer' THEN 1
ELSE 0
END) / SUM(CASE
WHEN action LIKE 'show' THEN 1
ELSE 0
END) AS rate, question_id
FROM survey_log
GROUP BY question_id
ORDER BY rate DESC;
SELECT question_id AS survey_log
FROM (
SELECT SUM(CASE
WHEN action LIKE 'answer' THEN 1
ELSE 0
END) / SUM(CASE
WHEN action LIKE 'show' THEN 1
ELSE 0
END) AS rate, question_id
FROM survey_log
GROUP BY question_id
ORDER BY rate DESC
LIMIT 1
) tmp;
将项目7中的employee表清空,重新插入以下数据(其实是多插入5,6两行):
编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:
此外,请考虑实现各部门前N高工资的员工功能。
DELETE FROM Employee;
INSERT INTO Employee(Id,Name,Salary,DepartmentId) VALUES
('1','Joe','70000','1'),
('2','Henry','80000','2'),
('3','Sam','60000',''2'),
('4','Max','90000','1'),
('5',"Janet",69000,1),
('6',"Randy",85000,1);
SELECT D1.NAME AS DepartmentId, E1.NAME AS employee, E1.Salary
FROM employee E1, employee E2, department D1
WHERE (E1.DepartmentId = E2.DepartmentId
AND E2.Salary >= E1.Salary
AND E1.DepartmentId = D1.Id)
GROUP BY E1.Name
HAVING COUNT(DISTINCT E2.Salary) <= 3
ORDER BY D1.Name, E1.Salary DESC;
point_2d 表包含一个平面内一些点(超过两个)的坐标值(x,y)。写一条查询语句求出这些点中的最短距离并保留2位小数。
最短距离是1,从点(-1,-1)到点(-1,2)。所以输出结果为:
注意: 所有点的最大距离小于10000。
// An highlighted block
var foo = 'bar';
CREATE TABLE point_2d (
x int,
y int
);
INSERT INTO point_2d VALUES
(-1, -1),(0, 0),(-1, -2);
SELECT MIN(distance) AS shortest
FROM (
SELECT POW(P1.x - p2.x, 2) + POW(p1.y - p2.y, 2) AS distance
FROM point_2d p1
INNER JOIN point_2d p2 ON !(p1.x = p2.x
AND p1.y = p2.y)
) as p
Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。
Users 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。
写一段 SQL 语句查出 2013年10月1日 至 2013年10月3日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。
// An highlighted block
var foo = 'bar';
DROP TABLE if EXISTS Trips;
DROP TABLE if EXISTS Users;
CREATE TABLE Users(
Users_Id INT UNIQUE,
Banned VARCHAR(5),
Role VARCHAR(10)
);
CREATE TABLE Trips(
Id INT UNIQUE,
Client_Id INT,
Driver_Id INT,
City_Id INT,
Status VARCHAR(30),
Request_at Date,
CONSTRAINT FOREIGN KEY (Client_Id) REFERENCES Users(Users_Id),
CONSTRAINT FOREIGN KEY(Driver_Id) REFERENCES Users(Users_Id)
);
INSERT INTO Users
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');
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, 11, 12, 'completed'
, '2013-10-03'),
(9, 3, 10, 12, 'completed'
, '2013-10-03'),
(10, 4, 13, 12, 'completed_by_driver'
, '2013-10-03');
SELECT Users_Id AS userid
FROM Users
WHERE Banned = 'Yes'
AND Role = 'client';
WHERE Client_id NOT IN (
SELECT Users_Id AS userid
FROM Users
WHERE Banned = 'Yes'
AND Role = 'client';
SELECT SUM(Status != 'completed') / COUNT(*)
FROM trips
GROUP BY Request_at
ELECT Request_at AS Day
, SUM(Status != 'completed') / COUNT(*) AS 'Cancellation Rate'
FROM trips
WHERE Client_Id NOT IN (
SELECT Users_Id AS userid
FROM Users
WHERE Banned = 'Yes'
AND Role = 'client'
)
AND DATEDIFF(Request_at, '2013-10-01') >= 0
AND DATEDiFF(Request_at, '2013-10-01') <= 2
GROUP BY Request_at
;