Skip to content

Commit 3c91770

Browse files
committed
update
1 parent d6e043c commit 3c91770

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

MySQL/combine-two-tables.sql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(n), n is size of Person Table
2+
# Space: O(n)
3+
#
4+
# Table: Person
5+
#
6+
# +-------------+---------+
7+
# | Column Name | Type |
8+
# +-------------+---------+
9+
# | PersonId | int |
10+
# | FirstName | varchar |
11+
# | LastName | varchar |
12+
# +-------------+---------+
13+
# PersonId is the primary key column for this table.
14+
# Table: Address
15+
#
16+
# +-------------+---------+
17+
# | Column Name | Type |
18+
# +-------------+---------+
19+
# | AddressId | int |
20+
# | PersonId | int |
21+
# | City | varchar |
22+
# | State | varchar |
23+
# +-------------+---------+
24+
# AddressId is the primary key column for this table.
25+
#
26+
# 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:
27+
#
28+
#FirstName, LastName, City, State
29+
#
30+
#
31+
# Write your MySQL query statement below
32+
SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address
33+
ON Person.PersonId=Address.PersonId
34+

MySQL/nth-highest-salary.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
#
4+
# Write a SQL query to get the nth highest salary from the Employee table.
5+
#
6+
# +----+--------+
7+
# | Id | Salary |
8+
# +----+--------+
9+
# | 1 | 100 |
10+
# | 2 | 200 |
11+
# | 3 | 300 |
12+
# +----+--------+
13+
# 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.
14+
#
15+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
16+
BEGIN
17+
RETURN (
18+
# Write your MySQL query statement below.
19+
SELECT MAX(Salary) /*This is the outer query part */
20+
FROM Employee Emp1
21+
WHERE (N-1) = ( /* Subquery starts here */
22+
SELECT COUNT(DISTINCT(Emp2.Salary))
23+
FROM Employee Emp2
24+
WHERE Emp2.Salary > Emp1.Salary)
25+
);
26+
END

MySQL/second-highest-salary.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
#
4+
# Write a SQL query to get the second highest salary from the Employee table.
5+
#
6+
# +----+--------+
7+
# | Id | Salary |
8+
# +----+--------+
9+
# | 1 | 100 |
10+
# | 2 | 200 |
11+
# | 3 | 300 |
12+
# +----+--------+
13+
# For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.
14+
#
15+
# Write your MySQL query statement below
16+
SELECT MAX(Salary) FROM Employee
17+
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee)

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
LeetCode
22
========
33

4-
Up to date (2015-01-06), there are total `174` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
4+
Up to date (2015-01-12), there are total `177` problems on [LeetCode Online Judge](https://oj.leetcode.com/).
55
The number of problems is increasing recently.
6-
Here is the classification of all `174` problems.
6+
Here is the classification of all `177` problems.
77
I'll keep updating for full summary and better solutions. Stay tuned for updates.
88

99
---
10+
Algorithm
11+
====
1012

1113
* [Bit Manipulation](https://github.com/kamyu104/LeetCode#bit-manipulation)
1214
* [Array](https://github.com/kamyu104/LeetCode#array)
@@ -29,6 +31,12 @@ I'll keep updating for full summary and better solutions. Stay tuned for updates
2931
* [Backtracking](https://github.com/kamyu104/LeetCode#backtracking)
3032
* [Greedy](https://github.com/kamyu104/LeetCode#greedy)
3133

34+
35+
DataBase
36+
===
37+
38+
* [SQL](https://github.com/kamyu104/LeetCode#sql)
39+
3240
---
3341

3442
##Bit Manipulation
@@ -698,3 +706,18 @@ Problem | Solution | Time | Space | Difficul
698706
[trapping-rain-water.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/trapping-rain-water.py
699707
[Wildcard Matching]:https://oj.leetcode.com/problems/wildcard-matching/
700708
[wildcard-matching.py]:https://github.com/kamyu104/LeetCode/blob/master/Python/wildcard-matching.py
709+
710+
##SQL
711+
Problem | Solution | Time | Space | Difficulty | Notes
712+
--------------- | --------------- | --------------- | --------------- | -------------- | -----
713+
[Combine Two Tables] | [combine-two-tables.sql] | _O(m + n)_ | _O(m + n)_ | Easy |
714+
[Nth Highest Salary] | [nth-highest-salary.sql] | _O(n^2)_ | _O(n)_ | Medium |
715+
[Second Highest Salary] | [second-highest-salary.sql] | _O(n)_ | _O(1)_ | Easy |
716+
717+
[Combine Two Tables]:https://oj.leetcode.com/problems/combine-two-tables/
718+
[combine-two-tables.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/combine-two-tables.sql
719+
[Nth Highest Salary]:https://oj.leetcode.com/problems/nth-highest-salary/
720+
[nth-highest-salary.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/nth-highest-salary.sql
721+
[Second Highest Salary]:https://oj.leetcode.com/problems/second-highest-salary/
722+
[second-highest-salary.sql]:https://github.com/kamyu104/LeetCode/blob/master/MySQL/second-highest-salary.sql
723+

0 commit comments

Comments
 (0)