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
- Diagnose, then practice. The diagnostic combines 14 query tasks with 11 concept checks and ends in a skill report. The practice set is organized by topic, and each topic opens with a lesson and moves from Easy to Hard. The first two topics cover ranking within groups and the growth, retention, and funnel metrics analysts report every week.
- Graded like an interview. Column names must match the prompt, a requested sort order requires an
ORDER BY, and missing rounding counts as wrong. Hidden tests target the edge cases that trip up medium-level answers: ties, NULLs, duplicate rows, and month and leap-year boundaries. Every problem also runs against a generated dataset of hundreds to thousands of rows, as on LeetCode. - Built to be reused. The project is an open-source template. Anyone can copy it, take the diagnostic, and add topics for their own gaps, using an AI assistant for the first draft and the built-in checks to verify it.
- Every requirement is tested. An automated check confirms that each requirement in every prompt is enforced by at least one test: 182 requirements across 29 problems.
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.
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.
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.
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:
- removes the final
ORDER BYand reverses each sort key - removes each
ROUND - applies problem-specific changes, such as dropping a date filter, changing
>=to>, swappingDENSE_RANKforRANK, or grouping by name instead of ID
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
- SQLite in the browser. The SQL engine runs client-side through sql.js (WebAssembly). There’s no server, no data leaves the page, and progress is saved on the device.
- MySQL syntax, as on LeetCode. MySQL functions such as
QUARTER,DATEDIFF, andDATE_ADD(d, INTERVAL 1 DAY)are rewritten into native SQLite expressions before a query runs. Registering them as JavaScript functions wasn’t an option: sql.js returns every JavaScript number as a float, soCONCAT('Q', QUARTER(d))produced'Q2.0'. - No hand-typed answers. Tests only add or remove rows. Expected results always come from running the reference solution on the same data, including any custom rows a user adds.
- No framework or build step. The site is plain HTML, CSS, and JavaScript, so a copy can be hosted anywhere, including for free on GitHub Pages.
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.


