No178 分数排名
编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。
+----+-------+
| 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 |
+-------+------+
思路:查出来有多少个比当前分数高的分数即可(去重加一)
答案:
select
Score,
(select count(distinct Score)+1 from Scores where Score>s.Score) as Rank
from Scores s
order by Score desc;
No180 至少出现三次的数字
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
思路:联表
答案1:
select distinct(l1.Num) as ConsecutiveNums
from
Logs l1,Logs l2,Logs l3
where l1.Id+1=l2.Id and l2.Id+1=l3.Id and l1.Num=l2.Num and l2.Num=l3.Num;
答案2:
select distinct(l1.Num) as ConsecutiveNums
from
Logs l1 left join Logs l2 on l1.Id+1=l2.Id left join Logs l3 on l1.Id+2=l3.Id
where l1.Num=l2.Num and l2.Num=l3.Num;
No177 第N高的薪水
编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
- 解法1:
- 思路:查询每个薪水对应的排名,找到排名=N的薪水
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
select Salary from Employee e where (select count(distinct Salary)+1 from Employee where Salary>e.Salary)=N limit 1
);
END
- 解法2
- 思路:按Salary排序找到排第N的薪水
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
select (IF((select count(distinct Salary) from Employee)>=N and N>0,(select min(Salary) from (select distinct Salary from Employee order by Salary desc limit N) e),NULL))
);
END
No184. 部门工资最高的员工
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 |
+------------+----------+--------+
解法:
# 1. 联表Employee,Department 筛选出Salary最高的Employee
select
d.Name as Department,e.Name as Employee,e.Salary as Salary
from
Employee e,Department d
where
e.DepartmentId=d.Id
and
(e.Salary,e.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId);
# 2. 联表Employee,Department,临时表(DepartmentId,max(Salary))
select
d.Name as Department,e.Name as Employee,e.Salary as Salary
from Employee e,(select max(Salary) as Salary,DepartmentId from Employee group by DepartmentId) as t1,Department d where e.DepartmentId=t1.DepartmentId and e.Salary=t1.Salary and e.DepartmentId=d.Id;
262. 行程和用户
Trips 表中存所有出租车的行程信息。每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。Status 是枚举类型,枚举成员为 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。
+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 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 表存所有用户。每个用户有唯一键 Users_Id。Banned 表示这个用户是否被禁止,Role 则是一个表示(‘client’, ‘driver’, ‘partner’)的枚举类型。
+----------+--------+--------+
| Users_Id | Banned | Role |
+----------+--------+--------+
| 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日 期间非禁止用户的取消率。基于上表,你的 SQL 语句应返回如下结果,取消率(Cancellation Rate)保留两位小数。
取消率的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+
解法:联表统计
select
t.Request_at as Day,
round(sum(if(t.Status='completed',0,1))/count(t.Status),2) as 'Cancellation Rate'
from
Trips t left join Users u1 on t.Client_Id=u1.Users_Id
left join Users u2 on t.Driver_Id=u2.Users_Id
where
t.Request_at>='2013-10-01'
and
t.Request_at<='2013-10-03'
and
u1.Banned='No'
and
u2.Banned='No'
group by t.Request_at;