1. 程式人生 > >【sql】leetcode習題 (共 42 題)

【sql】leetcode習題 (共 42 題)

【175】Combine Two Tables (2018年11月23日,開始集中review基礎)

Table: Person
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId is the primary key column for this table. 
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people.
FirstName, LastName, City, State 

題解:因為題目要求說person表裡面有的專案即使address表裡沒有也需要展示,所以用 left join

select Person.FirstName as FirstName, Person.LastName as LastName, Address.City as City, Address.State as State from Person left join Address on Person.PersonId = Address.PersonId;

 

【176】Second Highest Salary (第二高的工資)(2018年11月23日)

Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

注意,題目有個要求,如果沒有第二高的工資要返回 null,而不是空條目。還有一個問題就是如果表裡只有兩條,但是兩條的工資都是100, 這個需要返回 null,不是 100,所以要用 distinct

我一開始寫成了如下,但是沒有第二高的工資要返回 null 這個條件不滿足。所以 WA。

select Salary as SecondHighestSalary from Employee order by Salary desc limit 1, 1;

後來看了答案,答案說要重新搞一張表。(limit 1,1 和 limit 1 offset 1 是等價的)

select (select distinct Salary from Employee order by Salary desc limit 1 offset 1) as SecondHighestSalary;

 

【177】Nth Highest Salary 

【178】Rank Scores 

【180】Consecutive Numbers 

【181】Employees Earning More Than Their Managers 

【182】Duplicate Emails 

【183】Customers Who Never Order 

【184】Department Highest Salary 

【185】Department Top Three Salaries 

【196】Delete Duplicate Emails 

【197】Rising Temperature 

【262】Trips and Users 

【569】Median Employee Salary 

【570】Managers with at Least 5 Direct Reports 

【571】Find Median Given Frequency of Numbers 

【574】Winning Candidate 

【577】Employee Bonus 

【578】Get Highest Answer Rate Question 

【579】Find Cumulative Salary of an Employee 

【580】Count Student Number in Departments 

【584】Find Customer Referee 

【585】Investments in 2016 

【586】Customer Placing the Largest Number of Orders 

【595】Big Countries 

【596】Classes More Than 5 Students 

【597】Friend Requests I: Overall Acceptance Rate 

【601】Human Traffic of Stadium 

【602】Friend Requests II: Who Has the Most Friends 

【603】Consecutive Available Seats 

【607】Sales Person 

【608】Tree Node 

【610】Triangle Judgement 

【612】Shortest Distance in a Plane 

【613】Shortest Distance in a Line 

【614】Second Degree Follower 

【615】Average Salary: Departments VS Company 

【618】Students Report By Geography 

【619】Biggest Single Number 

【620】Not Boring Movies 

【626】Exchange Seats