Skip to content

Added database problems #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions LeetcodeProblems/Databases/Combine_Two_Tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Combine Two Tables
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
*/

SELECT Person.FirstName, Person.LastName, Address.City, Address.State
FROM Person LEFT JOIN Address ON Person.personID = Address.PersonID;
29 changes: 29 additions & 0 deletions LeetcodeProblems/Databases/Second_highest_salary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Second Highest Salary
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 |
+---------------------+
*/

SELECT IFNULL(
(
SELECT DISTINCT(Salary)
FROM employee
ORDER BY employee.Salary DESC
LIMIT 1
OFFSET 1
), NULL) AS "SecondHighestSalary"
34 changes: 34 additions & 0 deletions LeetcodeProblems/Databases/nth_Highest_Salary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Nth Highest Salary
https://leetcode.com/problems/nth-highest-salary/submissions/
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 |
+------------------------+
*/
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE n_offset INT;
SET n_offset = N -1;
RETURN (
SELECT DISTINCT(Salary)
FROM employee
ORDER BY Salary DESC
LIMIT 1
OFFSET n_offset
);
END
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ To run a specific problem in your console, go to the file test, add the call to
### Sorting Algorithms
| Algoritmhs |
| - |
| [Heap Sort](/SortingAlgorithms/heapSort.js) |
| [Heap Sort](/SortingAlgorithms/heapSort.js) |

### Databases
| Problems | Level | Link |
|-|-|-|
| [Combine Two Tables](/LeetcodeProblems/Databases/Combine_Two_Tables.sql) | Easy | https://leetcode.com/problems/combine-two-tables |
| [Second Highest Salary](/LeetcodeProblems/Databases/Second_highest_salary.sql)| Easy | https://leetcode.com/problems/second-highest-salary |
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |


### UtilsClasses
Expand Down