SQL Lab

Live: Diagnostic ↗ · Practice ↗ · Playground ↗ · Code: github.com/Mahabala-O/sql-lab ↗ · Tools: JavaScript, SQLite (sql.js, WebAssembly), MySQL syntax

SQL Lab is a free tool for improving at SQL interview problems. A diagnostic of 14 LeetCode-style tasks pinpoints where your SQL breaks down. A practice set then trains those gaps one topic at a time, starting with easy problems and building to hard ones. It runs entirely in the browser, and anyone can copy it to build practice around their own gaps.

Summary

The problem

I could solve most of LeetCode’s easy database problems, but medium problems took me far longer, especially those involving window functions. I couldn’t say exactly which concepts were slowing me down, and “practice more SQL” isn’t a plan. I wanted a tool that would tell me precisely what to work on.

My first diagnostic run answered that. It pointed to one pattern above all: ranking rows that are themselves aggregates, such as finding the top-selling product in each category with tie-breaks. That pattern is the core of problems like Seasonal Sales Analysis, so it became the first practice topic.

How it works

Every problem includes sample data, a reference solution, and hidden tests. Each hidden test adds a few rows designed to expose one specific mistake. When a hidden test fails, it opens as its own tab, showing the rows it added and letting you run your query against that data until it passes.

A failed hidden test opened as a tab. The query groups by product name, so two different products named Tee are counted as one.

Every problem also has a large hidden test: hundreds to thousands of generated rows with realistic messiness, such as customers who share a name, guest orders with no customer ID, ties at every level, and orders on both sides of every date range. The data is generated from a fixed seed, so it’s identical every time, and it can be copied as SQL to run elsewhere.

Each topic begins with a worked example. The lesson runs the query live and shows the output of every step, so you can see exactly what each stage of the query receives. The same step-by-step view is available in every problem.

The lesson for "Aggregate, then rank," showing the worked example and the output of each step

Problems increase in difficulty one element at a time. The second problem provides the full query except for the ranking step; later problems add a join, a CASE bucket, a second tie-break, and a second window function. Solved problems stay in the list with your best time, so you can return and solve them again.

The second topic, Growth, retention and funnels, follows the format of company interview questions on sites like StrataScratch: a business question, realistic tables from a fictional company, and a tag for the type of metric. The scenarios match the work I’ve done: no-show rates for a clinic network, active customers and revenue growth for an online store, and activation funnels and cohort retention for a SaaS product.

The practice set's problem list, from Easy to Hard

A third page, the playground, is for open-ended exploration: pick a dataset, such as 74 Hollywood films with real-world NULLs and inconsistent studio names, or paste any MySQL setup script, and run whatever you like without grading.

Testing the tests

Taking the first version of the diagnostic revealed a flaw: some answers passed even though they omitted an ORDER BY or a ROUND, because the sample data happened to be sorted and the averages happened to be whole numbers. An audit showed that 14 of the 44 requirements in the prompts had no test enforcing them.

I replaced that manual audit with an automated one. For every problem, the check breaks the reference solution one requirement at a time:

If no test catches a change, the check fails, and the problem doesn’t ship until the data exercises that requirement. All 182 requirements across 29 problems now pass, and the check runs on GitHub with every change.

Large datasets add a second risk: ties the prompt never settles. A query that returns a different but equally valid answer would fail unfairly. A separate check rebuilds every table with its rows stored in reverse order and confirms that each reference solution still gives the same answer. Where it didn’t, I added a tie-break to the prompt.

Two further checks protect the content. Each problem lists known wrong answers, including mistakes from my own attempts, that must fail a specific test. It also lists alternative correct answers that must pass, so the tests never accept only one way of writing a query.

Technical details

Use it yourself

The repository is a GitHub template. Copy it, enable GitHub Pages, and take the diagnostic. The guide in docs/tailor-with-ai.md provides prompts for turning your results into a ranked list of gaps, and then into a practice topic for the most important one. The built-in checks then review the draft, flagging any requirement without a test, any wrong answer that slips through, and any correct answer that’s rejected.

What’s next

Upcoming topics cover NULLs and anti-joins, consecutive runs (gaps and islands), and window-function details such as default frames. After that, I plan to add a visualization of how window functions partition, order, and frame rows, along with a re-test to measure progress.