leetcode中的sql

Posted limingqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode中的sql相关的知识,希望对你有一定的参考价值。

1 组合两张表

组合两张表, 题目很简单, 主要考察JOIN语法的使用。唯一需要注意的一点, 是题目中的这句话, "无论 person 是否有地址信息"。说明即使Person表, 没有信息我们也需要将Person表的内容进行返回。所以我选择使用左外查询, 当然你也可以选择RIGHT OUTER JOIN, 这取决于你查询语句的写法。

 

 

SELECT Person.FirstName, Person.LastName, Address.City, Address.State
FROM Person LEFT OUTER JOIN Address ON Person.PersonId = Address.Person


2: 第二高的薪水

第二高的薪水, 题目本身并不难, 但是请注意, 题目中的描述"如果不存在第二高的薪水,那么查询应返回 null", 这意味着, 如果SQL没有查询到结果, SQL本身需要一个默认的返回值。如何才能做到, 即使没有结果也返回一个值。

 


SELECT IFNULL( 
    (
        SELECT Employee.Salary
        FROM Employee
        GROUP BY Employee.Salary
        ORDER BY Employee.Salary DESC
        LIMIT 1 OFFSET 1
    ),
    NULL
) AS SecondHighestSalary;

 

3: 分数排名




select s1.Score,count(distinct s2.Score) as Rank
from Scores as s1 inner join Scores as s2
on s1.Score <= s2.Score
group by s1.Id
order by s1.Score desc

 

4: 超过经理收入的员工

SELECT emp1.Name AS Employee
FROM Employee AS emp1, Employee AS emp2
WHERE emp1.ManagerId = emp2.Id AND emp1.Salary > emp2.Salary
 

5: 查找重复的电子邮箱

同样是非常简单的一道题目, 唯一可能需要了解的就是, GROUP BY Person.Email的字句, 可以对Person.Email字段起到去重的作用

SELECT Person.Email AS Email
FROM Person
GROUP BY Person.Email
HAVING COUNT(Person.Email) > 1
-------------------------------

DELETE p1 FROM Person p1 inner join Person p on p1.Email = p2.Email AND p1.Id > p2.Id

6: 从不订购的客户

SELECT Customers.Name AS Customers
FROM Customers
WHERE Customers.Id NOT IN (
   SELECT Orders.CustomerId FROM Orders
)
--------------------------------------------

select Name as Customers from Customers where Id not in (select CustomerId from Orders)

7: 部门工资最高的员工

部门工资最高的员工, 在对这一题目进行解答之前。我们需要明确知道一点。"除聚合, 计算语句外,SELECT语句中的每个列都必须在GROUP BY子句中给出"。也就是说, 我们并不能在求, 每一个部门工资的Max最大值的时候, 把员工的id也计算出来。

对于这道题目,我们解答的步骤分为两步, 1. 求出每一个部门对应的最高工资, 并且将结果存储为派生表 2. 根据员工的部门id, 以及员工的工资, 与派生表联结, 比较对应员工的工资是否等于派生表的部门的最高工资。如果等于, 此人的工资就是部门的最高工资



select d.Name as Department,e.Name as Employee, e.Salary
from Employee as e inner join Department as d
on e.DepartmentId = d.Id
where (e.DepartmentId,e.salary) in (select DepartmentId,max(Salary) from Employee group by DepartmentId)

8: 删除重复的电子邮箱

 

DELETE p1 FROM Person p1 inner join Person p2 on p1.Email = p2.Email AND p1.Id > p2.Id

 

9: 上升的温度

SELECT w1.Id AS Id
FROM Weather AS w1 INNER JOIN Weather AS w2
ON w1.RecordDate = DATE_SUB(w2.RecordDate,INTERVAL -1 DAY)
WHERE w1.Temperature > w2.Temperature

10: 大的国家

SELECT World.Name AS Name, World.population AS population, World.area AS area
FROM World
WHERE World.population > 25000000 OR World.area > 3000000

11: 超过5名学生的课

超过5名学生的课, 本道题目注意考察点在于对GROUP BY去重效果的认知上。

首先子查询的采用嵌套分组。首先使用课程分组然后根据学生进行分组。可以有效去除课程, 学生重复的行。为什么不直接使用学生分组呢?因为这样做会丢失学生的课程信息。在外层的查询中只需要查找中COUNT大于5的课程即可

 

SELECT ClassLength.class FROM (
# 排除了学生和课程重复的行
    SELECT courses.class AS class
    FROM courses
    GROUP BY courses.class, courses.student
) AS ClassLength
GROUP BY ClassLength.class
HAVING COUNT(ClassLength.class) >= 5
 ------------------------------------------
select class from courses group by class having count(distinct(student))>=5;

优先级:from--on--join--where--group by--with--having--select--distinct--order by

12: 有趣的电影

 

select*from cinema where mod(id,2)=1 and description!=\'boring\' order by rating desc;

 

13: 交换工资

简单的case函数:

update salary

set 

    sex = CASE sex

   when \'m\' then \'f\'

  else \'m\'

  end;

------------------------------

搜索函数:推荐使用

update salary

set 

    sex = CASE 

   when \'m\' then \'f\'

  else \'m\'

  end;

 

以上是关于leetcode中的sql的主要内容,如果未能解决你的问题,请参考以下文章

sql sql里面的代码片段

Microsoft SQL Server 代码片段收集

sql Oracle代码片段

sql 日期转换代码片段 - Dato,120,konvertere

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

Discuz代码片段