My comprehensive solutions to the SQL 50 study plan on LeetCode. This repository features optimized queries written in MySQL, organized by topic according to the LeetCode curriculum.
The solutions are categorized into folders based on the specific SQL concepts they address:
| Category | Focus Areas |
|---|---|
| Select | Basic data retrieval and filtering (WHERE). |
| Basic Joins | Relational mapping using INNER, LEFT, and RIGHT JOIN. |
| Basic Aggregate Functions | Data summarization (COUNT, SUM, AVG, ROUND). |
| Sorting and Grouping | Result organization (GROUP BY, HAVING) and ordering. |
| Advanced Select and Joins | Complex joins, self-joins, and conditional logic (CASE WHEN). |
| Subqueries | Nested queries and Common Table Expressions (CTE). |
| Advanced String Functions / Regex | String manipulation and pattern matching. |
Throughout this challenge, I have mastered the following advanced SQL techniques:
- Window Functions: Implementing
RANK(),DENSE_RANK(),LEAD(), andLAG()for complex analytical tasks. - CTEs (Common Table Expressions): Writing clean, modular, and maintainable code using the
WITHclause. - Complex Filtering: Identifying duplicates or unique records using window-based counting (e.g., the Insurance problem).
- Running Totals: Calculating cumulative sums and weights (e.g., the Last Person to Fit in the Bus problem).
- Performance Optimization: Using set-based logic and efficient aggregation over procedural approaches.
| # | Problem | Key Concept | Link |
|---|---|---|---|
| 550 | Game Play Analysis IV | Retention Logic (RANK & LEAD) | View Script |
| 1661 | Average Time of Process per Machine | Time Diff with Lead Window Function | View Script |
| 1321 | Restaurant Growth | Moving Averages (ROWS BETWEEN) | View Script |
| 1907 | Count Salary Categories | Custom Categorization with UNION | View Script |
| 585 | Investments in 2016 | Multi-level Filtering (COUNT OVER) | View Script |
| 1204 | Last Person to Fit in the Bus | Running Totals (Cumulative Sum) | View Script |
All solutions have been verified on LeetCode and pass 100% of the test cases. To run these scripts locally:
- Ensure you have a MySQL environment set up (e.g., MySQL Workbench, DBeaver).
- Clone the repository:
git clone [https://github.com/Kuba27x/SQL50-Solutions.git](https://github.com/Kuba27x/SQL50-Solutions.git)