Summary of writing questions of LeetCode database module

Keywords: MySQL

Answer the selected databases are all MySQL. The following answers are not the best answers. If there is the best answer, please leave a message for communication~

Difficulty: simple

  • 175. Combining two tables
select p.FirstName,p.LastName,a.City,a.State from Person p
left join Address a on
p.PersonId=a.PersonId;
  • 176. Second highest salary
select IFNULL 
((select distinct Salary from Employee order by Salary DESC Limit 1,1),NULL)
as SecondHighestSalary;
  • 181. Employees in excess of manager's income
select a.Name as Employee from Employee a,Employee b 
where a.ManagerId=b.Id and a.Salary > b.Salary
  • 182. Find duplicate email
select Email from Person group by Email having Count(Email) > 1
  • 183. Customers who never order
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 duplicate email
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. Rising temperature
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. Big countries
select name,population,area from World where population > 25000000 or area > 3000000
  • 596. Lessons for more than five students
SELECT
    class
FROM
    (
        SELECT DISTINCT
            student,
            class
        FROM
            courses
    ) c
GROUP BY
    c.class
HAVING
    count(c.class) >= 5
  • 620. Interesting movies
select * from cinema where id%2=1 and description <> "boring" order by rating desc;
  • 627. Exchange of wages
UPDATE salary set sex = CASE WHEN (sex='f') THEN 'm' ELSE 'f' END

Posted by jonathen on Mon, 06 Jan 2020 01:54:47 -0800