【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 

猜你喜欢

转载自www.cnblogs.com/zhangwanying/p/9901918.html
今日推荐