├── LICENSE ├── README.md ├── big-countries └── README.md ├── classes-more-than-5-students └── README.md ├── combine-two-tables └── README.md ├── consecutive-numbers └── README.md ├── customers-who-never-order └── README.md ├── delete-duplicate-emails └── README.md ├── department-highest-salary └── README.md ├── department-top-three-salaries └── README.md ├── duplicate-emails └── README.md ├── employees-earning-more-than-their-managers └── README.md ├── exchange-seats └── README.md ├── human-traffic-of-stadium └── README.md ├── not-boring-movies └── README.md ├── nth-highest-salary └── README.md ├── rank-scores └── README.md ├── rising-temperature └── README.md ├── second-highest-salary └── README.md ├── swap-salary └── README.md └── trips-and-users └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 肖潇 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode-database-problem 2 | A solution for Leetcode database problem 3 | 4 | |Id|Question|Level| 5 | |--|--|--| 6 | |598|[Big Countries](big-countries)|Easy| 7 | |182|[Duplicate Emails](duplicate-emails)|Easy| 8 | |672|[Swap Salary](swap-salary)|Easy| 9 | |620|[Not Boring Movies](not-boring-movies)|Easy| 10 | |175|[Combine Two Tables](combine-two-tables)|Easy| 11 | |181|[Employees Earning More Than Their Managers](employees-earning-more-than-their-managers)|Easy| 12 | |183|[Customers Who Never Order](customers-who-never-order)|Easy| 13 | |196|[Delete Duplicate Emails](delete-duplicate-emails)|Easy| 14 | |197|[Rising Temperature](rising-temperature)|Easy| 15 | |596|[Classes More Than 5 Students](classes-more-than-5-students)|Easy| 16 | |176|[Second Highest Salary](second-highest-salary)|Easy| 17 | |626|[Exchange Seats](exchange-seats)|Medium| 18 | |178|[Rank Scores](rank-scores)|Medium| 19 | |180|[Consecutive Numbers](consecutive-numbers)|Medium| 20 | |177|[Nth Highest Salary](nth-highest-salary)|Medium| 21 | |184|[Department Highest Salary](department-highest-salary)|Medium| 22 | |601|[Human Traffic of Stadium](human-traffic-of-stadium)|Hard| 23 | |262|[Trips and Users](trips-and-users)|Hard| 24 | |185|[Department Top Three Salaries](department-top-three-salaries)|Hard| -------------------------------------------------------------------------------- /big-countries/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | There is a table World 4 | 5 | 6 | | name | continent | area | population | gdp | 7 | |--|--|--|--|--| 8 | | Afghanistan | Asia | 652230 | 25500100 | 20343000 | 9 | | Albania | Europe | 28748 | 2831741 | 12960000 | 10 | | Algeria | Africa | 2381741 | 37100000 | 188681000 | 11 | | Andorra | Europe | 468 | 78115 | 3712000 | 12 | | Angola | Africa | 1246700 | 20609294 | 100990000 | 13 | 14 | A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million. 15 | 16 | Write a SQL solution to output big countries' name, population and area. 17 | 18 | For example, according to the above table, we should output: 19 | 20 | 21 | | name | population | area | 22 | |--|--|--| 23 | | Afghanistan | 25500100 | 652230 | 24 | | Algeria | 37100000 | 2381741 | 25 | 26 | ### Solution 27 | 28 | ```sql 29 | select `name`, `population`, `area` from `World` 30 | where `area` > 3000000 or `population` > 25000000; 31 | ``` 32 | 33 | ### Note 34 | 35 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /classes-more-than-5-students/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | There is a table courses with columns: student and class 4 | 5 | Please list out all classes which have more than or equal to 5 students. 6 | 7 | For example, the table: 8 | 9 | | student | class | 10 | |---------|------------| 11 | | A | Math | 12 | | B | English | 13 | | C | Math | 14 | | D | Biology | 15 | | E | Math | 16 | | F | Computer | 17 | | G | Math | 18 | | H | Math | 19 | | I | Math | 20 | 21 | Should output: 22 | 23 | | class | 24 | |---------| 25 | | Math | 26 | 27 | Note: 28 | The students should not be counted duplicate in each course. 29 | 30 | ### Solution 31 | 32 | ```sql 33 | select `class` from `courses` group by `class` 34 | having count(distinct `student`) >= 5; 35 | ``` 36 | 37 | ### Note 38 | 39 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /combine-two-tables/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Table: Person 4 | 5 | | Column Name | Type | 6 | |-------------|---------| 7 | | PersonId | int | 8 | | FirstName | varchar | 9 | | LastName | varchar | 10 | 11 | PersonId is the primary key column for this table. 12 | Table: Address 13 | 14 | | Column Name | Type | 15 | |-------------|---------| 16 | | AddressId | int | 17 | | PersonId | int | 18 | | City | varchar | 19 | | State | varchar | 20 | 21 | AddressId is the primary key column for this table. 22 | 23 | 24 | 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: 25 | 26 | FirstName, LastName, City, State 27 | 28 | ### Solution 29 | 30 | ```sql 31 | select `FirstName`, `LastName`, `City`, `State` from `Person` left join `Address` 32 | on `Person`.`PersonId` = `Address`.`PersonId`; 33 | ``` 34 | 35 | ### Note 36 | 37 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /consecutive-numbers/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Write a SQL query to find all numbers that appear at least three times consecutively. 4 | 5 | | Id | Num | 6 | |----|-----| 7 | | 1 | 1 | 8 | | 2 | 1 | 9 | | 3 | 1 | 10 | | 4 | 2 | 11 | | 5 | 1 | 12 | | 6 | 2 | 13 | | 7 | 2 | 14 | 15 | For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times. 16 | 17 | | ConsecutiveNums | 18 | |-----------------| 19 | | 1 | 20 | 21 | ### Solution 22 | 23 | ```sql 24 | select distinct `l4`.`Num` as `ConsecutiveNums` from `Logs` `l1`, `Logs` `l2`, `Logs` `l3`, `Logs` `l4` 25 | where `l1`.`Num` = `l2`.`Num` and 26 | `l2`.`Num` = `l3`.`Num` 27 | and `l1`.`Id` + 1 = `l2`.`Id` 28 | and `l2`.`Id` + 1 = `l3`.`Id` 29 | and `l4`.`Id` in (`l1`.`Id`, `l2`.`Id`, `l3`.`Id`); 30 | ``` 31 | 32 | ### Note 33 | 34 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /customers-who-never-order/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything. 4 | 5 | Table: Customers. 6 | 7 | | Id | Name | 8 | |----|-------| 9 | | 1 | Joe | 10 | | 2 | Henry | 11 | | 3 | Sam | 12 | | 4 | Max | 13 | 14 | Table: Orders. 15 | 16 | | Id | CustomerId | 17 | |----|------------| 18 | | 1 | 3 | 19 | | 2 | 1 | 20 | 21 | Using the above tables as example, return the following: 22 | 23 | | Customers | 24 | |-----------| 25 | | Henry | 26 | | Max | 27 | 28 | ### Solution 29 | 30 | ```sql 31 | select `Name` as `Customers` from `Customers` 32 | where `Id` not in 33 | (select `CustomerId` from `Orders`); 34 | ``` 35 | 36 | ### Note 37 | 38 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /delete-duplicate-emails/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. 4 | 5 | | Id | Email | 6 | |----|------------------| 7 | | 1 | john@example.com | 8 | | 2 | bob@example.com | 9 | | 3 | john@example.com | 10 | 11 | Id is the primary key column for this table. 12 | For example, after running your query, the above Person table should have the following rows: 13 | 14 | | Id | Email | 15 | |----|------------------| 16 | | 1 | john@example.com | 17 | | 2 | bob@example.com | 18 | 19 | Note: 20 | 21 | Your output is the whole Person table after executing your sql. Use delete statement. 22 | 23 | ### Solution 24 | 25 | ```sql 26 | delete `p1` from `Person` `p1`, `Person` `p2` where 27 | `p1`.`Email` = `p2`.`Email` 28 | and `p1`.`Id` > `p2`.`Id`; 29 | ``` 30 | 31 | ### Note 32 | 33 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /department-highest-salary/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. 3 | 4 | | Id | Name | Salary | DepartmentId | 5 | |----|-------|--------|--------------| 6 | | 1 | Joe | 70000 | 1 | 7 | | 2 | Henry | 80000 | 2 | 8 | | 3 | Sam | 60000 | 2 | 9 | | 4 | Max | 90000 | 1 | 10 | 11 | The Department table holds all departments of the company. 12 | 13 | | Id | Name | 14 | |----|----------| 15 | | 1 | IT | 16 | | 2 | Sales | 17 | 18 | Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department. 19 | 20 | | Department | Employee | Salary | 21 | |------------|----------|--------| 22 | | IT | Max | 90000 | 23 | | Sales | Henry | 80000 | 24 | 25 | ### Solution 26 | 27 | ```sql 28 | select `Department`.`Name` as `Department`, `e1`.`Name` as `Employee`, 29 | `e1`.`Salary` from `Employee` `e1` join `Department` on `Department`.`Id` = `e1`.`DepartmentId` 30 | where `e1`.`Salary` >= (select max(`Salary`) from `Employee` where `Employee`.`DepartmentId` = `e1`.`DepartmentId`); 31 | ``` 32 | 33 | ### Note 34 | 35 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /department-top-three-salaries/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. 3 | 4 | | Id | Name | Salary | DepartmentId | 5 | |----|-------|--------|--------------| 6 | | 1 | Joe | 70000 | 1 | 7 | | 2 | Henry | 80000 | 2 | 8 | | 3 | Sam | 60000 | 2 | 9 | | 4 | Max | 90000 | 1 | 10 | | 5 | Janet | 69000 | 1 | 11 | | 6 | Randy | 85000 | 1 | 12 | 13 | The Department table holds all departments of the company. 14 | 15 | | Id | Name | 16 | |----|----------| 17 | | 1 | IT | 18 | | 2 | Sales | 19 | 20 | Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows. 21 | 22 | | Department | Employee | Salary | 23 | |------------|----------|--------| 24 | | IT | Max | 90000 | 25 | | IT | Randy | 85000 | 26 | | IT | Joe | 70000 | 27 | | Sales | Henry | 80000 | 28 | | Sales | Sam | 60000 | 29 | 30 | ### Solution 31 | 32 | ```sql 33 | select `d`.`Name` as `Department`, `e1`.`Name` as `Employee`, `e1`.`Salary` from `Employee` `e1`, 34 | `Employee` `e2`, `Department` `d` 35 | where `e1`.`DepartmentId` = `e2`.`DepartmentId` 36 | and `e1`.`Salary` <= `e2`.`Salary` 37 | and `d`.`Id` = `e1`.`DepartmentId` 38 | group by `e1`.`Id` 39 | having count(distinct `e2`.`Salary`) <= 3 40 | order by `Department`, `Salary` desc;; 41 | ``` 42 | 43 | ### Note 44 | 45 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /duplicate-emails/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Write a SQL query to find all duplicate emails in a table named Person. 4 | 5 | | Id | Email | 6 | |--|--| 7 | | 1 | a@b.com | 8 | | 2 | c@d.com | 9 | | 3 | a@b.com | 10 | 11 | For example, your query should return the following for the above table: 12 | 13 | | Email | 14 | |--| 15 | | a@b.com | 16 | 17 | Note: All emails are in lowercase. 18 | 19 | ### Solution 20 | 21 | ```sql 22 | select `Email` from `Person` group by `Email` having count(*) > 1; 23 | ``` 24 | 25 | ### Note 26 | 27 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /employees-earning-more-than-their-managers/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. 4 | 5 | | Id | Name | Salary | ManagerId | 6 | |----|-------|--------|-----------| 7 | | 1 | Joe | 70000 | 3 | 8 | | 2 | Henry | 80000 | 4 | 9 | | 3 | Sam | 60000 | NULL | 10 | | 4 | Max | 90000 | NULL | 11 | 12 | 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. 13 | 14 | | Employee | 15 | |----------| 16 | | Joe | 17 | 18 | ### Solution 19 | 20 | ```sql 21 | select `e1`.`Name` as `Employee` from `Employee` `e1`, `Employee` `e2` 22 | where `e1`.`ManagerId` = `e2`.`Id` 23 | and `e1`.`Salary` > `e2`.`Salary`; 24 | ``` 25 | 26 | ### Note 27 | 28 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /exchange-seats/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids. 4 | 5 | The column id is continuous increment. 6 | Mary wants to change seats for the adjacent students. 7 | Can you write a SQL query to output the result for Mary? 8 | 9 | | id | student | 10 | |---------|---------| 11 | | 1 | Abbot | 12 | | 2 | Doris | 13 | | 3 | Emerson | 14 | | 4 | Green | 15 | | 5 | Jeames | 16 | 17 | For the sample input, the output is: 18 | 19 | | id | student | 20 | |---------|---------| 21 | | 1 | Doris | 22 | | 2 | Abbot | 23 | | 3 | Green | 24 | | 4 | Emerson | 25 | | 5 | Jeames | 26 | 27 | Note: 28 | If the number of students is odd, there is no need to change the last one's seat. 29 | 30 | ### Solution 31 | 32 | ```sql 33 | select * from ( 34 | select `id` + 1 as `id`, `student` from `seat` 35 | where `id`% 2 = 1 and `id` + 1 <= (select count(*) from `seat`) 36 | union 37 | select `id` - 1 as `id`, `student` from `seat` 38 | where `id`% 2 = 0 39 | union 40 | select `id`, `student` from `seat` 41 | where `id`% 2 = 1 and `id` + 1 > (select count(*) from `seat`) 42 | ) as `a` order by id; 43 | ``` 44 | 45 | ### Note 46 | 47 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /human-traffic-of-stadium/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people 3 | 4 | Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive). 5 | 6 | For example, the table stadium: 7 | 8 | | id | date | people | 9 | |------|------------|-----------| 10 | | 1 | 2017-01-01 | 10 | 11 | | 2 | 2017-01-02 | 109 | 12 | | 3 | 2017-01-03 | 150 | 13 | | 4 | 2017-01-04 | 99 | 14 | | 5 | 2017-01-05 | 145 | 15 | | 6 | 2017-01-06 | 1455 | 16 | | 7 | 2017-01-07 | 199 | 17 | | 8 | 2017-01-08 | 188 | 18 | 19 | For the sample data above, the output is: 20 | 21 | | id | date | people | 22 | |------|------------|-----------| 23 | | 5 | 2017-01-05 | 145 | 24 | | 6 | 2017-01-06 | 1455 | 25 | | 7 | 2017-01-07 | 199 | 26 | | 8 | 2017-01-08 | 188 | 27 | 28 | Note: 29 | Each day only have one row record, and the dates are increasing with id increasing. 30 | 31 | ### Solution 32 | 33 | ```sql 34 | select distinct `s4`.* from `stadium` `s1`, `stadium` `s2`, `stadium` `s3`, `stadium` `s4` 35 | where `s1`.`people` >= 100 36 | and `s2`.`people` >= 100 37 | and `s3`.`people` >= 100 38 | and `s1`.`id` + 1 = `s2`.`id` 39 | and `s2`.`id` + 1 = `s3`.`id` 40 | and `s4`.`id` in (`s1`.`id`, `s2`.`id`, `s3`.`id`);; 41 | ``` 42 | 43 | ### Note 44 | 45 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /not-boring-movies/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions. 4 | Please write a SQL query to output movies with an odd numbered ID and a description that is not 'boring'. Order the result by rating. 5 | 6 | For example, table cinema: 7 | 8 | | id | movie | description | rating | 9 | |---------|-----------|--------------|-----------| 10 | | 1 | War | great 3D | 8.9 | 11 | | 2 | Science | fiction | 8.5 | 12 | | 3 | irish | boring | 6.2 | 13 | | 4 | Ice song | Fantacy | 8.6 | 14 | | 5 | House card| Interesting| 9.1 | 15 | 16 | For the example above, the output should be: 17 | 18 | | id | movie | description | rating | 19 | |---------|-----------|--------------|-----------| 20 | | 5 | House card| Interesting| 9.1 | 21 | | 1 | War | great 3D | 8.9 | 22 | 23 | 24 | ### Solution 25 | 26 | ```sql 27 | select * from `cinema` where `id` % 2 = 1 and `description` != 'boring' 28 | order by `rating` desc; 29 | ``` 30 | 31 | ### Note 32 | 33 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /nth-highest-salary/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | Write a SQL query to get the nth highest salary from the Employee table. 3 | 4 | | Id | Salary | 5 | |----|--------| 6 | | 1 | 100 | 7 | | 2 | 200 | 8 | | 3 | 300 | 9 | 10 | 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. 11 | 12 | | getNthHighestSalary(2) | 13 | |------------------------| 14 | | 200 | 15 | 16 | ### Solution 17 | 18 | ```sql 19 | 20 | BEGIN 21 | declare m int; 22 | set m = n - 1; 23 | RETURN ( 24 | # Write your MySQL query statement below. 25 | select ifnull( 26 | (select distinct `Salary` from `Employee` order by `Salary` desc limit m,1) 27 | , null) as `getNthHighestSalary` 28 | 29 | ); 30 | END 31 | ``` 32 | 33 | ### Note 34 | 35 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /rank-scores/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks. 4 | 5 | | Id | Score | 6 | |----|-------| 7 | | 1 | 3.50 | 8 | | 2 | 3.65 | 9 | | 3 | 4.00 | 10 | | 4 | 3.85 | 11 | | 5 | 4.00 | 12 | | 6 | 3.65 | 13 | 14 | For example, given the above Scores table, your query should generate the following report (order by highest score): 15 | 16 | | Score | Rank | 17 | |-------|------| 18 | | 4.00 | 1 | 19 | | 4.00 | 1 | 20 | | 3.85 | 2 | 21 | | 3.65 | 3 | 22 | | 3.65 | 3 | 23 | | 3.50 | 4 | 24 | 25 | ### Solution 26 | 27 | ```sql 28 | select `s1`.`Score`, ( 29 | select count(distinct `Score`) from `Scores` `s2` where `s2`.`Score` >= `s1`.`Score` 30 | ) as `Rank` from `Scores` `s1` order by `Score` desc; 31 | ``` 32 | 33 | ### Note 34 | 35 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /rising-temperature/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. 4 | 5 | | Id(INT) | RecordDate(DATE) | Temperature(INT) | 6 | |---------|------------------|------------------| 7 | | 1 | 2015-01-01 | 10 | 8 | | 2 | 2015-01-02 | 25 | 9 | | 3 | 2015-01-03 | 20 | 10 | | 4 | 2015-01-04 | 30 | 11 | 12 | For example, return the following Ids for the above Weather table: 13 | 14 | | Id | 15 | |----| 16 | | 2 | 17 | | 4 | 18 | 19 | ### Solution 20 | 21 | ```sql 22 | select `w2`.`Id` from `Weather` `w1`, `Weather` `w2` 23 | where datediff(`w2`.`RecordDate`, `w1`.`RecordDate`) = 1 24 | and `w2`.`Temperature` > `w1`.`Temperature`; 25 | ``` 26 | 27 | ### Note 28 | 29 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /second-highest-salary/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Write a SQL query to get the second highest salary from the Employee table. 4 | 5 | | Id | Salary | 6 | |----|--------| 7 | | 1 | 100 | 8 | | 2 | 200 | 9 | | 3 | 300 | 10 | 11 | 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. 12 | 13 | | SecondHighestSalary | 14 | |---------------------| 15 | | 200 | 16 | 17 | ### Solution 18 | 19 | ```sql 20 | select ifnull(( 21 | select distinct `Salary` from `Employee` order by `Salary` desc limit 1,1 22 | ), null) as SecondHighestSalary; 23 | ``` 24 | 25 | ### Note 26 | 27 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /swap-salary/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table. 4 | For example: 5 | 6 | | id | name | sex | salary | 7 | |----|------|-----|--------| 8 | | 1 | A | m | 2500 | 9 | | 2 | B | f | 1500 | 10 | | 3 | C | m | 5500 | 11 | | 4 | D | f | 500 | 12 | 13 | After running your query, the above salary table should have the following rows: 14 | 15 | | id | name | sex | salary | 16 | |----|------|-----|--------| 17 | | 1 | A | f | 2500 | 18 | | 2 | B | m | 1500 | 19 | | 3 | C | f | 5500 | 20 | | 4 | D | m | 500 | 21 | 22 | 23 | ### Solution 24 | 25 | ```sql 26 | update `salary` set `sex` = if(`sex` = 'f', 'm', 'f'); 27 | ``` 28 | 29 | ### Note 30 | 31 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. -------------------------------------------------------------------------------- /trips-and-users/README.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’). 3 | 4 | | Id | Client_Id | Driver_Id | City_Id | Status |Request_at| 5 | |----|-----------|-----------|---------|--------------------|----------| 6 | | 1 | 1 | 10 | 1 | completed |2013-10-01| 7 | | 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01| 8 | | 3 | 3 | 12 | 6 | completed |2013-10-01| 9 | | 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01| 10 | | 5 | 1 | 10 | 1 | completed |2013-10-02| 11 | | 6 | 2 | 11 | 6 | completed |2013-10-02| 12 | | 7 | 3 | 12 | 6 | completed |2013-10-02| 13 | | 8 | 2 | 12 | 12 | completed |2013-10-03| 14 | | 9 | 3 | 10 | 12 | completed |2013-10-03| 15 | | 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03| 16 | 17 | The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’). 18 | 19 | | Users_Id | Banned | Role | 20 | |----------|--------|--------| 21 | | 1 | No | client | 22 | | 2 | Yes | client | 23 | | 3 | No | client | 24 | | 4 | No | client | 25 | | 10 | No | driver | 26 | | 11 | No | driver | 27 | | 12 | No | driver | 28 | | 13 | No | driver | 29 | 30 | Write a SQL query to find the cancellation rate of requests made by unbanned users between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places. 31 | 32 | | Day | Cancellation Rate | 33 | |------------|-------------------| 34 | | 2013-10-01 | 0.33 | 35 | | 2013-10-02 | 0.00 | 36 | | 2013-10-03 | 0.50 | 37 | 38 | ### Solution 39 | 40 | ```sql 41 | select `t1`.`Day` as `Day`, round(ifnull(`t2`.`num`/`t1`.`num`, 0), 2) as `Cancellation Rate` from 42 | (select `Trips`.`Request_at` as `Day`, count(*) as `num` from `Trips` join `Users` 43 | on `Users`.`Users_Id` = `Trips`.`Client_Id` 44 | and `Users`.`Banned` = 'No' 45 | and `Trips`.`Request_at` in ('2013-10-01', '2013-10-02', '2013-10-03') 46 | group by `Trips`.`Request_at` 47 | ) as `t1` 48 | left join 49 | (select `Trips`.`Request_at` as `Day`, count(*) as `num` from `Trips` join `Users` 50 | on `Users`.`Users_Id` = `Trips`.`Client_Id` 51 | and `Users`.`Banned` = 'No' 52 | and `Trips`.`Status` != 'completed' 53 | and `Trips`.`Request_at` in ('2013-10-01', '2013-10-02', '2013-10-03') 54 | group by `Trips`.`Request_at` 55 | ) as `t2` 56 | on `t1`.`Day` = `t2`.`Day` 57 | order by `Day` asc;; 58 | ``` 59 | 60 | ### Note 61 | 62 | If you have a better solution, you can star and fork [Leetcode-database-problem](https://github.com/xx19941215/Leetcode-database-problem) then create a pull request. --------------------------------------------------------------------------------