You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Every problem has the schema, sample data, a working query, a step-by-step breakdown, and a personal takeaway so you know why it works β not just that it does.
π Why This Repo?
Most SQL solution repos dump queries in a .sql file and call it done.
This one is different. Every single problem is its own documented file β with the table schema, sample input/output, the SQL solution, a numbered approach walking through the logic, the concepts it exercises, and a personal takeaway distilled from actually solving it. Think of it as the notes you wish someone had written before you started.
Who is this for?
π Students preparing for placements / internships
πΌ Anyone brushing up SQL for data analyst / data engineering roles
ποΈ People who want to understand why a query works, not just copy it
The most commonly tested patterns β pulled from these 50 problems.
-- β Safe NULL comparison (never use = NULL)WHERE column IS NULLWHERE column IS NOT NULL-- β LEFT JOIN to include non-matching rowsSELECTa.id, b.valueFROM TableA a
LEFT JOIN TableB b ONa.id=b.a_idWHEREb.a_id IS NULL-- rows in A with NO match in B-- β SUM(condition) trick β count rows matching a conditionSELECTSUM(status ='confirmed') /COUNT(*) AS rate -- MySQL booleanFROM Orders
-- β DENSE_RANK for top-N per group (no gaps on ties)SELECT*FROM (
SELECT name, salary,
DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS rnk
FROM Employee
) t
WHERE rnk <=3-- β Moving average β last N rows inclusiveSELECTAVG(amount) OVER (
ORDER BY visit_date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS moving_avg
-- β UNION ALL to combine then aggregate (preserve duplicates)SELECT id FROM TableA
UNION ALLSELECT id FROM TableB
-- β GROUP_CONCAT for comma-separated strings per groupSELECT sell_date, COUNT(DISTINCT product) AS num_sold,
GROUP_CONCAT(DISTINCT product ORDER BY product) AS products
FROM Activities
GROUP BY sell_date
-- β Second highest salary (handles NULL edge case)SELECT IFNULL(
(SELECT DISTINCT salary FROM Employee ORDER BY salary DESCLIMIT1 OFFSET 1),
NULL
) AS SecondHighestSalary
-- β DELETE with self-join (avoid correlated subquery trap)DELETE p1
FROM Person p1
JOIN Person p2 ONp1.email=p2.emailANDp1.id>p2.id
π Progress
β Easy ββββββββββββββββββββββββββββββββ 32 / 32
β Medium βββββββββββββββββ 17 / 17
β Hard β 1 / 1
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
Total ββββββββββββββββββββββββββββββββββ 50 / 50
π€ Contributing
Found a better approach or a bug in my solution? PRs are welcome.
Fork the repo
Create a branch: git checkout -b fix/problem-number
Commit your change: git commit -m "fix: improved approach for #1934"
Open a pull request
If this helped you, a β keeps it alive.
About
SQL solutions for LeetCode 50 problems with structured and readable queries. Categorized by difficulty for systematic SQL practice and learning.