Skip to content

Commit 2de59a5

Browse files
Added database problems
1 parent 45cab44 commit 2de59a5

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Combine Two Tables
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+
27+
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:
28+
29+
FirstName, LastName, City, State
30+
*/
31+
32+
SELECT Person.FirstName, Person.LastName, Address.City, Address.State
33+
FROM Person LEFT JOIN Address ON Person.personID = Address.PersonID;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Second Highest Salary
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 query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
14+
15+
+---------------------+
16+
| SecondHighestSalary |
17+
+---------------------+
18+
| 200 |
19+
+---------------------+
20+
*/
21+
22+
SELECT IFNULL(
23+
(
24+
SELECT DISTINCT(Salary)
25+
FROM employee
26+
ORDER BY employee.Salary DESC
27+
LIMIT 1
28+
OFFSET 1
29+
), NULL) AS "SecondHighestSalary"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Nth Highest Salary
3+
4+
https://leetcode.com/problems/nth-highest-salary/submissions/
5+
6+
Write a SQL query to get the nth highest salary from the Employee table.
7+
8+
+----+--------+
9+
| Id | Salary |
10+
+----+--------+
11+
| 1 | 100 |
12+
| 2 | 200 |
13+
| 3 | 300 |
14+
+----+--------+
15+
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.
16+
17+
+------------------------+
18+
| getNthHighestSalary(2) |
19+
+------------------------+
20+
| 200 |
21+
+------------------------+
22+
*/
23+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
24+
BEGIN
25+
DECLARE n_offset INT;
26+
SET n_offset = N -1;
27+
RETURN (
28+
SELECT DISTINCT(Salary)
29+
FROM employee
30+
ORDER BY Salary DESC
31+
LIMIT 1
32+
OFFSET n_offset
33+
);
34+
END

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ To run a specific problem in your console, go to the file test, add the call to
7676
### Sorting Algorithms
7777
| Algoritmhs |
7878
| - |
79-
| [Heap Sort](/SortingAlgorithms/heapSort.js) |
79+
| [Heap Sort](/SortingAlgorithms/heapSort.js) |
80+
81+
### Databases
82+
| Problems | Level | Link |
83+
|-|-|-|
84+
| [Combine Two Tables](/LeetcodeProblems/Databases/Combine_Two_Tables.sql) | Easy | https://leetcode.com/problems/combine-two-tables |
85+
| [Second Highest Salary](/LeetcodeProblems/Databases/Second_highest_salary.sql)| Easy | https://leetcode.com/problems/second-highest-salary |
86+
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |
8087

8188

8289
### UtilsClasses

0 commit comments

Comments
 (0)