LeetCode 数据库模块刷题纪要

解答所选数据库均为MySQL,以下解答并非最佳解答,若有最佳解答,欢迎留言交流~

难度:简单

  • 175.组合两个表
select p.FirstName,p.LastName,a.City,a.State from Person p
left join Address a on
p.PersonId=a.PersonId;
  • 176.第二高的薪水
select IFNULL 
((select distinct Salary from Employee order by Salary DESC Limit 1,1),NULL)
as SecondHighestSalary;
  • 181.超过经理收入的员工
select a.Name as Employee from Employee a,Employee b 
where a.ManagerId=b.Id and a.Salary > b.Salary
  • 182.查找重复的电子邮箱
select Email from Person group by Email having Count(Email) > 1
  • 183.从不订购的客户
select Name as Customers from Customers where Id not in
(select c.Id from Orders o
left join Customers c
on c.Id=o.CustomerId)
  • 196.删除重复的电子邮箱
DELETE from Person WHERE NOT EXISTS (
SELECT c.Id FROM
    (SELECT Min(Id) as Id FROM Person GROUP BY Email)c
     WHERE c.Id=person.Id);
  • 197.上升的温度
SELECT
    weather.Id
FROM
    (
        SELECT
            DATE_ADD(w.RecordDate, INTERVAL 1 DAY) AS RecordDate,
            w.Temperature
        FROM
            weather w
    ) c,
    weather
WHERE
    c.RecordDate = weather.RecordDate
AND c.Temperature < weather.Temperature;
  • 595.大的国家
select name,population,area from World where population > 25000000 or area > 3000000
  • 596.超过5名学生的课
SELECT
    class
FROM
    (
        SELECT DISTINCT
            student,
            class
        FROM
            courses
    ) c
GROUP BY
    c.class
HAVING
    count(c.class) >= 5
  • 620.有趣的电影
select * from cinema where id%2=1 and description <> "boring" order by rating desc;
  • 627.交换工资
UPDATE salary set sex = CASE WHEN (sex='f') THEN 'm' ELSE 'f' END

你可能感兴趣的:(数据库,LeetCode,数据库)