diff --git a/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second Highest Salary.SQL b/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second Highest Salary.SQL deleted file mode 100644 index e8f8028..0000000 --- a/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second Highest Salary.SQL +++ /dev/null @@ -1,66 +0,0 @@ -176. Second Highest Salary -Solved -Medium -Topics -Companies -SQL Schema -Pandas Schema -Table: Employee - -+-------------+------+ -| Column Name | Type | -+-------------+------+ -| id | int | -| salary | int | -+-------------+------+ -id is the primary key (column with unique values) for this table. -Each row of this table contains information about the salary of an employee. - - -Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas). - -The result format is in the following example. - - - -Example 1: - -Input: -Employee table: -+----+--------+ -| id | salary | -+----+--------+ -| 1 | 100 | -| 2 | 200 | -| 3 | 300 | -+----+--------+ -Output: -+---------------------+ -| SecondHighestSalary | -+---------------------+ -| 200 | -+---------------------+ -Example 2: - -Input: -Employee table: -+----+--------+ -| id | salary | -+----+--------+ -| 1 | 100 | -+----+--------+ -Output: -+---------------------+ -| SecondHighestSalary | -+---------------------+ -| null | -+---------------------+ - -WITH - RankedEmployees AS ( - SELECT *, DENSE_RANK() OVER(ORDER BY salary DESC) AS `rank` - FROM Employee - ) -SELECT MAX(salary) AS SecondHighestSalary -FROM RankedEmployees -WHERE `rank` = 2; \ No newline at end of file diff --git a/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second Highest Salary.py b/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second Highest Salary.py new file mode 100644 index 0000000..e50a809 --- /dev/null +++ b/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second Highest Salary.py @@ -0,0 +1,6 @@ +import pandas as pd + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + unique_salaries = employee['salary'].drop_duplicates().sort_values(ascending=False) + second_highest = unique_salaries.iloc[1] if len(unique_salaries) > 1 else None + return pd.DataFrame({'SecondHighestSalary': [second_highest]}) diff --git a/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second heighest salary.sql b/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second heighest salary.sql index 3d17f1d..1ac922f 100644 --- a/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second heighest salary.sql +++ b/LeetCode SQL 50 Solution/176. Second heighest salary/176. Second heighest salary.sql @@ -1,4 +1,17 @@ - +# Solution 1 # Write your MSSQL query statement below select max(salary) as SecondHighestSalary from employee where salary not in (select max(salary) from employee) + + + +# Solution 2 +# Write your MSSQL query statement below +# Write your MySQL query statement below +SELECT + ( + SELECT DISTINCT salary + FROM Employee + ORDER BY salary DESC + LIMIT 1, 1 + ) AS SecondHighestSalary; \ No newline at end of file diff --git a/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md b/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md new file mode 100644 index 0000000..0eb0146 --- /dev/null +++ b/LeetCode SQL 50 Solution/176. Second heighest salary/readme.md @@ -0,0 +1,121 @@ +# 🏆 LeetCode 176: Second Highest Salary (Pandas) 🚀 + +## **Problem Statement** +You are given an **Employee** table with the following schema: + +``` ++-------------+------+ +| 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`. + +--- + +## **Example 1** + +### **Input:** +```plaintext ++----+--------+ +| id | salary | ++----+--------+ +| 1 | 100 | +| 2 | 200 | +| 3 | 300 | ++----+--------+ +``` +### **Output:** +```plaintext ++---------------------+ +| SecondHighestSalary | ++---------------------+ +| 200 | ++---------------------+ +``` + +--- + +## **Example 2** + +### **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`. + +--- + +## **Code (Pandas Solution)** + +```python +import pandas as pd + +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]}) +``` + +--- + +## **Complexity Analysis** +- **Time Complexity:** $$O(n \log n)$$ due to sorting. +- **Space Complexity:** $$O(n)$$ for storing unique salaries. + +--- + +## **Why This Works?** +✅ Uses Pandas' `drop_duplicates()` for distinct values. +✅ Sorts efficiently using `sort_values()`. +✅ Handles cases where no second highest salary exists. + +--- + +## **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` + FROM Employee +) +SELECT MAX(salary) AS SecondHighestSalary +FROM RankedEmployees +WHERE `rank` = 2; +``` + +--- + +### **Happy Coding! 🚀💡** +Hope this helps! Feel free to ⭐ **star** this repo if you found it useful. 😊 \ No newline at end of file