力扣(Leecode)数据库-部分题目

/* 
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
FirstName, LastName, City, State
表1: Person
列名         类型
PersonId    int
FirstName    varchar
LastName    varchar
PersonId 是上表主键

表2: Address
列名            类型
AddressId    int
PersonId    int
City    varchar
State    varchar

 */

select FirstName, LastName, City, State
from Person p
left join address a
on p.PersonId = a.PersonId

/*
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary).
潜台词是如果不存在第二高的薪水,那么查询应返回 null
*/

select IFNULL( (select DISTINCT Salary from Employee ORDER BY Salary DESC LIMIT 1 ,1), null)
as secondhighestSalary

/*
分数排名(178)—中等

编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,
平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。结果示例:
| 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) from Scores where  score >=s1.score)   as rank 
from Scores s2
order by score desc;


/*
连续出现的数字(180)—中等

编写一个 SQL 查询,查找所有至少连续出现三次的数字。
| Id | Num |
这里的join指的是inner join
*/
两种解法
一是利用join

select DISTINCT l1.num from Logs l1
join Logs l2 on l1.id = l2.id-1
join Logs l3 on l2.id = l3.id -1
where l1.num = l2.num and l2.num = l3.num;


一种是不使用连接,用条件判断

select DISTINCT l1.num
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;


/*
超过经理收入的员工(181)—简单
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 是唯一一个收入超过他的经理的员工。
*/
两种解法
一种是join

select E1.Name 
from Employee E1 join Employee E2 on E1.ManagerId = E2.Id
where E1.Salary > E2.Salary


一种是不使用连接,用条件判断

select E1.Name
from Employee E1,Employee E2
where E1.ManagerId = E2.Id and E1.Salary > E2.Salary;

/*
7.查找重复的电子邮箱(182)—简单
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
| Id | Email |
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
根据以上输入,你的查询应返回以下结果:
| Email |
| [email protected] |
说明:所有电子邮箱都是小写字母。
*/

select DISTINCT(Email)
from Person
group by Email
having count(Email) > 1;

第二种

select DISTINCT(p1.Email)
from Person p1,Person p2
where p1.Email = p2.Email
and p1.id != p2.id;

 

/*
8.从不订购的客户(183)—简单

某网站包含两个表,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 C.name from Customers C left join Orders O 
on C.Id = O.CustomerId
where O.CustomerId = null;


一种是使用not in 但时间有点长

select name from Customers C
where C.id not in(
    select CustomerId from Orders);

/*
9.部门工资最高的员工(184)—中等
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 |
*/
方式一:用in 和子查询

select D.Name as Department, E.Name as Name, Salary
from Employee E join Department D 
on E.DepartmentId = D.Id
where (E.DepartmentId, Salary) in
    (select DepartmentId, Salary from Employee 
    group by DepartmentId)


方式二:用>=all()

select D.Name as Department, E.Name as Name, Salary
from Employee E join Department D 
on E.DepartmentId = D.Id
where E.Salary >= all(
select Salary from Employee E2
where E.DepartmentId = E2.DepartmentId);


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章