Welcome to my complete SQL repository! This single document contains all my SQL cheat sheets, foundational notes, and structured solutions to popular LeetCode database problems.
To get the most out of this repository, follow this recommended learning sequence:
- Read
start.docFirst: Open and review thestart.docfile in this repository. It covers core database concepts, relational schemas, basic syntax, and data types before you jump into practical problems. - Follow the Topic Sections: Work through the SQL sections below in order. They progress naturally from basic data retrieval to advanced analytical queries.
- Solve LeetCode SQL 50: Then visit the LeetCode 50 Days SQL challenge and solve them; all the answers and concepts are written here.
-
- Select
-
- Basic Joins
-
- Basic Aggregate Functions
-
- Sorting and Grouping
-
- Advanced Select and Joins
-
- Subqueries
-
- Advanced String Functions / Regex / Clause
Covers basic column extraction, filtering rows using WHERE, conditional expressions using CASE WHEN, and handling NULL values.
Covers combining data across multiple tables using INNER JOIN, LEFT JOIN, RIGHT JOIN, and self-joins.
Key Rule (
UNIONvsUNION ALL):
Always useUNION ALLif you want to keep duplicate rows or guarantee that two identical results both appear in your output. StandardUNIONautomatically removes duplicate rows across the combined dataset.
Covers summarizing datasets using COUNT(), SUM(), AVG(), MIN(), MAX(), and filtering aggregated results with HAVING.
Covers ordering results with ORDER BY, grouping data with GROUP BY, and eliminating duplicate rows using DISTINCT.
Covers complex join logic, consecutive row matching, non-equi joins, and multi-table filtering.
Covers scalar subqueries, correlated subqueries, IN / NOT IN clauses, and using Common Table Expressions (WITH ... AS) to break down complex queries into readable blocks.
Window functions allow you to perform calculations across a set of table rows related to the current row without collapsing the dataset into a single summary row.
ROW_NUMBER(): Numbers every row sequentially (1, 2, 3, 4) with zero duplicates.RANK(): Competition ranking (1, 2, 2, 4) — leaves numerical gaps after ties.DENSE_RANK(): Category ranking (1, 2, 2, 3) — leaves no gaps after ties. Use this for "Top N Unique Values" problems like LeetCode 185.
When calculating moving metrics (such as a 7-day rolling average in LeetCode 1321), use:
SUM(amount) OVER (ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
This explicitly instructs SQL to look back at the last 6 calendar days plus the current day to calculate an accurate 7-day total.
Covers text operations like CONCAT(), SUBSTRING(), LOWER(), UPPER(), TRIM(), pattern matching with LIKE, and regular expression matching with REGEXP.
When writing and debugging queries, remember the order in which the database executes your code behind the scenes:
FROM/JOIN— Identifies and joins source tables.WHERE— Filters raw rows before grouping or windowing (Column aliases created in SELECT do not exist yet!).GROUP BY— Groups rows together.HAVING— Filters grouped records.SELECT— Calculates final columns, aliases, and window functions likeDENSE_RANK().DISTINCT— Eliminates duplicate output rows.ORDER BY— Sorts the final result set.LIMIT— Restricts the total number of output rows.