diff --git a/LeetcodeProblems/Databases/Combine_Two_Tables.sql b/LeetcodeProblems/Databases/Combine_Two_Tables.sql new file mode 100644 index 0000000..8731fa8 --- /dev/null +++ b/LeetcodeProblems/Databases/Combine_Two_Tables.sql @@ -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; diff --git a/LeetcodeProblems/Databases/Second_highest_salary.sql b/LeetcodeProblems/Databases/Second_highest_salary.sql new file mode 100644 index 0000000..ea53459 --- /dev/null +++ b/LeetcodeProblems/Databases/Second_highest_salary.sql @@ -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" diff --git a/LeetcodeProblems/Databases/nth_Highest_Salary.sql b/LeetcodeProblems/Databases/nth_Highest_Salary.sql new file mode 100644 index 0000000..5f81360 --- /dev/null +++ b/LeetcodeProblems/Databases/nth_Highest_Salary.sql @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index ba3cb71..ee5e909 100644 --- a/README.md +++ b/README.md @@ -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