日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【sql】leetcode习题 (共 42 题)

發布時間:2024/1/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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?(2018年11月23日)

Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null. +------------------------+ | getNthHighestSalary(2) | +------------------------+ | 200 | +------------------------+

題意就是返回第 N 高的工資。和上面一題很像。注意點就是不能直接寫 limit N-1, 1 語法會出錯。

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN #limit m, n 表示的是從第 m 個條目(m is 0 based)開始的 n 條, 如果下面直接用 N-1 的話不行的,語法錯誤。DECLARE M INT;SET M = N - 1;RETURN (select (select distinct Salary from Employee order by Salary desc limit M, 1)); END

?

【178】Rank Scores?(2018年11月23日)

?

【180】Consecutive Numbers?(2018年11月23日)?

Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times. +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+

題解:本來不會寫,后來找了題解:https://my.oschina.net/Tsybius2014/blog/494823

可以用 select, 也可以 join, 還有一種通解的寫法(如果把 3 延長到 N怎么辦)

select distinct L1.Num as ConsecutiveNums from Logs L1, Logs L2, Logs L3 where (L1.Id + 1 = L2.Id AND L1.Num = L2.Num) AND (L1.Id + 2 = L3.Id AND L2.Num = L3.Num)

?

?

【181】Employees Earning More Than Their Managers?

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. +----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager. +----------+ | Employee | +----------+ | Joe | +----------+

題解:別名的使用。

select e.Name as Employee from Employee as e, Employee as m where e.ManagerId = m.Id and e.Salary > m.Salary

?

【182】Duplicate Emails?(group by ... having ..子句, 2018年11月23日)

Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+For example, your query should return the following for the above table: +---------+ | Email | +---------+ | a@b.com | +---------+ Note: All emails are in lowercase.

題解:

select Email from Person group by Email having count(Email) > 1

?

【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?(2018年11月24日)

?

Table: Candidate +-----+---------+ | id | Name | +-----+---------+ | 1 | A | | 2 | B | | 3 | C | | 4 | D | | 5 | E | +-----+---------+ Table: Vote +-----+--------------+ | id | CandidateId | +-----+--------------+ | 1 | 2 | | 2 | 4 | | 3 | 3 | | 4 | 2 | | 5 | 5 | +-----+--------------+ id is the auto-increment primary key, CandidateId is the id appeared in Candidate table. Write a sql to find the name of the winning candidate, the above example will return the winner B. +------+ | Name | +------+ | B | +------+ Notes: You may assume there is no tie, in other words there will be at most one winning candidate.

?

題解:注意 group by 和 order by 一起使用的時候,order by 中的列必須要出現在 group by 中。(solution里面還有別的方法)

select Name from Candidate where id = (select CandidateId from Vote group by CandidateId order by count(CandidateId) desc limit 1);

?

?

?【577】Employee Bonus?(2018年11月23日)

Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+-----------+--------+ | empId | name | supervisor| salary | +-------+--------+-----------+--------+ | 1 | John | 3 | 1000 | | 2 | Dan | 3 | 2000 | | 3 | Brad | null | 4000 | | 4 | Thomas | 3 | 4000 | +-------+--------+-----------+--------+ empId is the primary key column for this table. Table: Bonus +-------+-------+ | empId | bonus | +-------+-------+ | 2 | 500 | | 4 | 2000 | +-------+-------+ empId is the primary key column for this table. Example ouput: +-------+-------+ | name | bonus | +-------+-------+ | John | null | | Dan | 500 | | Brad | null | +-------+-------+

題解:left join語句,還用到了 sql 的三值邏輯(true, false, unkown)

select emp.name as name, bon.bonus as bonus from Employee as emp left join Bonus as bon on emp.empId = bon.empId where bon.bonus < 1000 or bon.bonus is null

?

【578】Get Highest Answer Rate Question?

【579】Find Cumulative Salary of an Employee?

【580】Count Student Number in Departments?

?

【584】Find Customer Referee?(2018年11月23日)?

Given a table customer holding customers information and the referee. +------+------+-----------+ | id | name | referee_id| +------+------+-----------+ | 1 | Will | NULL | | 2 | Jane | NULL | | 3 | Alex | 2 | | 4 | Bill | NULL | | 5 | Zack | 1 | | 6 | Mark | 2 | +------+------+-----------+ Write a query to return the list of customers NOT referred by the person with id '2'. For the sample data above, the result is: +------+ | name | +------+ | Will | | Jane | | Bill | | Zack | +------+

題解:本題需要了解的知識點是, sql 的三值邏輯: true, false, unkown。一切和 null 比較的值都是 unkown, 包括 null 本身。所以 sql 提供了 'is null' 和 'is not null' 這兩個關鍵詞。

select name from customer where referee_id <> 2 or referee_id is null;

如果條件中只有 referee_id <> 2 這一個條件的話,那么只會返回 Zack 這一個結果。

?

【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?

轉載于:https://www.cnblogs.com/zhangwanying/p/9901918.html

總結

以上是生活随笔為你收集整理的【sql】leetcode习题 (共 42 题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。