Leetcode超全数据库SQL练习汇总Leetcode 数据库SQL练习
表1: Person
+-------------+---------+
|列名|类型|
+-------------+---------+
| PersonId    |int|
| FirstName  |varchar|
| LastName    |varchar|
+-------------+---------+
PersonId 是上表主键
表2: Address
+-------------+---------+
|列名|类型|
+-------------+---------+
| AddressId  |int|
| PersonId    |int|
| City        |varchar|
| State      |varchar|
+-------------+---------+
AddressId 是上表主键
编写⼀个SQL查询,满⾜条件:⽆论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:FirstName, LastName, City, State
select FirstName,LastName,City,state
from Person left join Address
on Person.PersonId = Address.PersonId
编写⼀个SQL查询,获取 Employee 表中第⼆⾼的薪⽔(Salary)。
+----+--------+
| Id | Salary |
+----+--------+
|1|100|
|2|200|
|3|300|
+----+--------+
例如上述 Employee 表,SQL查询应该返回200作为第⼆⾼的薪⽔。如果不存在第⼆⾼的薪⽔,那么查询应返回null。
+---------------------+
| SecondHighestSalary |
+---------------------+
|200|
+---------------------+
select ifnull(
(select distinct salary from Employee order by salary desc limit1,1),NULL)
AS SecondHighestSalary
编写⼀个SQL查询,获取 Employee 表中第 n ⾼的薪⽔(Salary)。
+----+--------+
| Id | Salary |
+----+--------+
|1|100|
|2|200|
|3|300|
+----+--------+
例如上述 Employee 表,n =2时,应返回第⼆⾼的薪⽔200。如果不存在第 n ⾼的薪⽔,那么查询应返回null。
+------------------------+
| getNthHighestSalary(2)|
+------------------------+
|200|
+------------------------+
CREATE FUNCTION getNthHighestSalary(N INT)RETURNS INT
BEGIN
RETURN(
# Write your MySQL query statement below.
select ifnull(Salary,NULL)
from
(
select distinct Salary, dense_rank()over(order by Salary desc) rn
from Employee
)t
where rn=N
)
;
END
编写⼀个SQL查询来实现分数排名。
如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下⼀个名次应该是下⼀个连续的整数值。换句话说,名次之间不应该有“间隔”。
+----+-------+
| Id | Score |
+----+-------+
|1|3.50|
|2|3.65|
|3|4.00|
|4|3.85|
|5|4.00|
|6|3.65|
+----+-------+
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从⾼到低排列):mysql删除重复的数据保留一条
+-------+------+
| Score | Rank |
+-------+------+
|4.00|1|
|4.00|1|
|3.85|2|
|3.65|3|
|3.65|3|
|3.50|4|
+-------+------+
重要提⽰:对于 MySQL 解决⽅案,如果要转义⽤作列名的保留字,可以在关键字之前和之后使⽤撇号。例如`Rank`
select Score,dense_rank()over(order by score desc)'Rank'
from scores;
表:Logs
+-------------+---------+
|Column Name |Type|
+-------------+---------+
| id          |int|
| num        |varchar|
+-------------+---------+
id 是这个表的主键。
编写⼀个SQL查询,查所有⾄少连续出现三次的数字。
返回的结果表中的数据可以按任意顺序排列。
查询结果格式如下⾯的例⼦所⽰:
Logs 表:
+----+-----+
| Id | Num |
+----+-----+
|1|1|
|2|1|
|3|1|
|4|2|
|5|1|
|6|2|
|7|2|
+----+-----+
Result 表:
+-----------------+
| ConsecutiveNums |
+-----------------+
|1|
+-----------------+
1是唯⼀连续出现⾄少三次的数字。
select distinct(num)as ConsecutiveNums
from
(
select id,num,(row_number()over(order by id)- row_number()over(partition by num order by id))as id_rn from logs
)t
group by id_rn,t.num
having count(1)>=3;
Employee 表包含所有员⼯,他们的经理也属于员⼯。每个员⼯都有⼀个 Id,此外还有⼀列对应员⼯的经理的 Id。
+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
|1| Joe  |70000|3|
|2| Henry |80000|4|
|3| Sam  |60000|NULL|
|4| Max  |90000|NULL|
+----+-------+--------+-----------+
给定 Employee 表,编写⼀个SQL查询,该查询可以获取收⼊超过他们经理的员⼯的姓名。在上⾯的表格中,Joe 是唯⼀⼀个收⼊超过他的经理的员⼯。
+----------+
| Employee |
+----------+
| Joe      |
+----------+
select a.Name as Employee
from employee a inner join employee b
on a.ManagerId = b.Id
where a.salary > b.salary
编写⼀个SQL查询,查 Person 表中所有重复的电⼦邮箱。
⽰例:
+----+---------+
| Id | Email  |
+----+---------+
|1| a@b|
|2| c@d|
|3| a@b|
+----+---------+
根据以上输⼊,你的查询应返回以下结果:
+---------+
| Email  |
+---------+
| a@b|
+---------+
说明:所有电⼦邮箱都是⼩写字母。
select Email from person
group by Email having count(1)>1
某⽹站包含两个表,Customers 表和 Orders 表。编写⼀个SQL查询,出所有从不订购任何东西的客户。Customers 表:
+----+-------+
| Id | Name  |
+----+-------+
|1| Joe  |
|2| Henry |
|3| Sam  |
|4| Max  |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
|1|3|
|2|1|
+----+------------+
例如给定上述表格,你的查询应返回:
+-----------+
| Customers |
+-----------+
| Henry    |
| Max      |
+-----------+
select customers.name as Customers
from customers
where customers.id not in
(
select customerid from orders
)
;