diff --git a/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md b/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md index 0eb0146..76e32e9 100644 --- a/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md +++ b/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md @@ -1,113 +1,65 @@ -# 🏆 LeetCode 176: Second Highest Salary (Pandas) 🚀 +# 176. Second Highest Salary -## **Problem Statement** -You are given an **Employee** table with the following schema: +## Problem Statement +You are given a table `Employee` containing the salaries of employees. The goal is to find the second highest distinct salary from this table. If there is no second highest salary, return `NULL` (or `None` in Pandas). + +### Table: Employee -``` -+-------------+------+ | Column Name | Type | -+-------------+------+ +| ----------- | ---- | | id | int | | salary | int | -+-------------+------+ -``` -- `id` is the **primary key** (unique for each employee). -- Each row contains information about an employee's salary. -Your task is to **find the second highest distinct salary**. -- If there is no second highest salary, return `None`. +- `id` is the primary key for this table. +- Each row contains salary information for an employee. ---- +## Example 1: -## **Example 1** - -### **Input:** -```plaintext -+----+--------+ -| id | salary | -+----+--------+ -| 1 | 100 | -| 2 | 200 | -| 3 | 300 | -+----+--------+ -``` -### **Output:** -```plaintext -+---------------------+ -| SecondHighestSalary | -+---------------------+ -| 200 | -+---------------------+ -``` +### **Input:** ---- +| id | salary | +| --- | ------ | +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | -## **Example 2** +### **Output:** -### **Input:** -```plaintext -+----+--------+ -| id | salary | -+----+--------+ -| 1 | 100 | -+----+--------+ -``` -### **Output:** -```plaintext -+---------------------+ | SecondHighestSalary | -+---------------------+ -| null | -+---------------------+ -``` - ---- - -## **Approach** - -1. **Remove duplicate salaries** using `.drop_duplicates()`. -2. **Sort salaries** in descending order. -3. **Retrieve the second highest salary**, if it exists. -4. If there is no second highest salary, return `None`. - ---- +| ------------------- | +| 200 | -## **Code (Pandas Solution)** +## Example 2: -```python -import pandas as pd +### **Input:** -def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: - # Get distinct salaries, sorted in descending order - unique_salaries = employee['salary'].drop_duplicates().sort_values(ascending=False) - - # Check if there is a second highest salary - second_highest = unique_salaries.iloc[1] if len(unique_salaries) > 1 else None - - # Return as a DataFrame with column name 'SecondHighestSalary' - return pd.DataFrame({'SecondHighestSalary': [second_highest]}) -``` +| id | salary | +| --- | ------ | +| 1 | 100 | ---- +### **Output:** -## **Complexity Analysis** -- **Time Complexity:** $$O(n \log n)$$ due to sorting. -- **Space Complexity:** $$O(n)$$ for storing unique salaries. +| SecondHighestSalary | +| ------------------- | +| NULL | --- +## **Approach** -## **Why This Works?** -✅ Uses Pandas' `drop_duplicates()` for distinct values. -✅ Sorts efficiently using `sort_values()`. -✅ Handles cases where no second highest salary exists. +### **SQL Approach** +1. **Use a Window Function:** + - Apply `DENSE_RANK()` to rank salaries in descending order. + - Assign rank `1` to the highest salary, `2` to the second highest, and so on. +2. **Filter by Rank:** + - Select the salary where `rank = 2`. + - If no second highest salary exists, return `NULL`. --- +## **Solution** -## **SQL Equivalent Solution** -If solving in SQL, we can use **`DENSE_RANK()`**: ```sql WITH RankedEmployees AS ( - SELECT salary, DENSE_RANK() OVER(ORDER BY salary DESC) AS `rank` + SELECT *, DENSE_RANK() OVER(ORDER BY salary DESC) AS `rank` FROM Employee ) SELECT MAX(salary) AS SecondHighestSalary @@ -116,6 +68,32 @@ WHERE `rank` = 2; ``` --- +## **File Structure** +``` +📂 SecondHighestSalary +├── 📄 README.md # Problem statement, approach, and solution +├── 📄 solution.sql # SQL query file +├── 📄 solution_pandas.py # Pandas solution file +``` + +--- +## **Alternative Pandas Approach** + +```python +import pandas as pd + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + unique_salaries = employee['salary'].drop_duplicates().nlargest(2) + second_highest = unique_salaries.iloc[1] if len(unique_salaries) > 1 else None + return pd.DataFrame({'SecondHighestSalary': [second_highest]}) +``` + +--- +## **Resources & References** +- [LeetCode Problem Link](https://leetcode.com/problems/second-highest-salary/) +- [SQL DENSE_RANK() Documentation](https://www.sqlservertutorial.net/sql-server-window-functions/sql-server-dense_rank-function/) + +--- +## **Contribute** +Feel free to contribute by submitting an issue or a pull request! -### **Happy Coding! 🚀💡** -Hope this helps! Feel free to ⭐ **star** this repo if you found it useful. 😊 \ No newline at end of file diff --git a/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.py b/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.py new file mode 100644 index 0000000..9136333 --- /dev/null +++ b/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.py @@ -0,0 +1,7 @@ +import pandas as pd + +def getNthHighestSalary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + unique_salaries = employee['salary'].drop_duplicates().nlargest(N) + if len(unique_salaries) < N: + return pd.DataFrame({"getNthHighestSalary(N)": [None]}) + return pd.DataFrame({"getNthHighestSalary(N)": [unique_salaries.iloc[-1]]}) \ No newline at end of file diff --git a/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.sql b/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.sql index a79d1d5..6dfb0ac 100644 --- a/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.sql +++ b/LeetCode SQL 50 Solution/177. Nth Highest Salary/177. Nth Highest Salary.sql @@ -1,28 +1,12 @@ -/* -* Order By Clause - * ORDER BY order_by_expression - [ COLLATE collation_name ] - [ ASC | DESC ] - [ ,...n ] -[ ] - - ::= -{ - OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS } - [ - FETCH { FIRST | NEXT } {integer_constant | fetch_row_count_expression } { ROW | ROWS } ONLY - ] -} -*/ - -Create FUNCTION getNthHighestSalary(@N INT) returns INT as +# Write your MySQL query statement below. +CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN - Return( - Select Salary - From Employee - Gourp By Salary - Order By Salary DESC - Offset @N-1 rows - Fetch First 1 Rows Only +SET N = N-1; + RETURN ( + SELECT DISTINCT(salary) from Employee order by salary DESC + LIMIT 1 OFFSET N + ); - End +END + +# pls upvote if you find solution easy to undestand....!! Thanks..!!! \ No newline at end of file diff --git a/LeetCode SQL 50 Solution/177. Nth Highest Salary/readme.md b/LeetCode SQL 50 Solution/177. Nth Highest Salary/readme.md new file mode 100644 index 0000000..4d13d7e --- /dev/null +++ b/LeetCode SQL 50 Solution/177. Nth Highest Salary/readme.md @@ -0,0 +1,121 @@ +# 177. Nth Highest Salary + +## Problem Statement +Given a table `Employee`, write a SQL query to find the `nth` highest salary. If there is no `nth` highest salary, return `null`. + +### Table Schema: `Employee` +| Column Name | Type | +| ----------- | ---- | +| id | int | +| salary | int | + +- `id` is the primary key (unique values for employees). +- `salary` column contains employee salary details. + +### Example 1: +#### **Input:** +```sql ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | ++----+--------+ +n = 2 +``` +#### **Output:** +```sql ++------------------------+ +| getNthHighestSalary(2) | ++------------------------+ +| 200 | ++------------------------+ +``` + +### Example 2: +#### **Input:** +```sql ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | ++----+--------+ +n = 2 +``` +#### **Output:** +```sql ++------------------------+ +| getNthHighestSalary(2) | ++------------------------+ +| null | ++------------------------+ +``` + +--- + +## Approach +1. Use the `DENSE_RANK()` function to rank salaries in descending order. +2. Filter for the `nth` highest salary using a `WHERE` clause. +3. If there is no `nth` highest salary, return `NULL`. + +--- + +## SQL Solution +```sql +CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT AS +BEGIN + RETURN ( + SELECT DISTINCT salary FROM Employee + ORDER BY salary DESC + LIMIT 1 OFFSET N-1 + ); +END; +``` + +### Explanation: +- `ORDER BY salary DESC` sorts salaries in descending order. +- `LIMIT 1 OFFSET N-1` fetches the `nth` highest salary. +- If `N` is larger than the number of salaries, `NULL` is returned. + +--- + +## Pandas Solution +```python +import pandas as pd + +def getNthHighestSalary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + unique_salaries = employee['salary'].drop_duplicates().nlargest(N) + if len(unique_salaries) < N: + return pd.DataFrame({"getNthHighestSalary(N)": [None]}) + return pd.DataFrame({"getNthHighestSalary(N)": [unique_salaries.iloc[-1]]}) +``` + +### Explanation: +- `drop_duplicates()` removes duplicate salaries. +- `nlargest(N)` gets the `N` highest salaries. +- If `N` is greater than available salaries, return `None`. + +--- + +## File Structure +``` +📂 nth_highest_salary + ├── 📄 README.md # Problem statement, approach, solution + ├── 📄 nth_highest_salary.sql # SQL Solution + ├── 📄 nth_highest_salary.py # Pandas Solution + └── 📄 example_input_output.txt # Sample input & expected output +``` + +--- + +## References +- [LeetCode Problem #177](https://leetcode.com/problems/nth-highest-salary/) +- [MySQL Documentation - LIMIT & OFFSET](https://dev.mysql.com/doc/refman/8.0/en/select.html) +- [Pandas Documentation](https://pandas.pydata.org/docs/) + +--- + +### Contributors +- **[Antim Pal]** 🚀 +