Skip to content
azure-ellerPublic

About

LeetCode for SQL, in your terminal

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

sqlgym

Practice data engineering interview problems from the terminal, in your editor: SQL against DuckDB, the same problems with DataFrames (pandas or polars), and plain Python problems (parsing logs, flattening JSON, ordering a DAG...).

sqlgym: the start menu, then a Python problem: a wrong answer, then the right one

Needs Neovim 0.10+ and uv.

uv tool install git+https://github.com/azure-eller/sqlgym
sqlgym               # pick a mode and a level, then practice
sqlgym open 107      # open a specific problem (--df: with DataFrames)
sqlgym list          # all problems and your progress
sqlgym check FILE    # check a file's answer from the shell

sqlgym starts with a menu: j/k (or ↑/↓) move between Mode and Level, h/l (or ←/→) change them, Enter starts. Then it opens Neovim on two pages: the problem with your SQL below it, and your SQL with the results below it. Running a query shows the results; the SQL pane fits your query and the page gets the rest of the screen.

Key
<leader>r run your query and check it
<leader>n next problem (skips this one if unsolved)
<leader>a show the answer
<leader>R redo: clear your SQL (u undoes)
Tab flip between the problem and the results
Esc / q back to the problem from the results
? show these keys
Ctrl-j / Ctrl-k scroll the problem or results down / up
:q save and quit

The same actions are available as :Sqlgym run|next|answer|redo. The keys use your own leader and exist only in sqlgym's buffers, so your Neovim config is never changed. In those buffers sqlgym also turns off format-on-save, completion popups and sqlfluff linting, which get in the way of practice.

Only the last statement in your SQL is checked, so you can keep scratch queries above your answer. Wide tables and results scroll sideways (zl / zh).

DataFrame mode

Pick DataFrames in the menu (or sqlgym open 107 --df) to answer the SQL problems in Python. Each problem starts you off with

import polars as pl  # or: import pandas as pd

def solve(users):
    return

solve() gets the tables it names as DataFrames: polars, or pandas if your file imports only pandas. Return a DataFrame, a Series or a single value. It's checked against the SQL answer, so the same rules apply: columns by position, floats to 4 places, and pandas' index is ignored unless it's named (as after a groupby, where it becomes a column). print() output shows above your result.

Python mode

Python problems (201+) are plain Python, as in the coding round of many DE interviews: you write solve() with the arguments the problem names, and it's run on several cases, each checked against the reference solution's result. Dicts compare in any key order, lists and tuples alike, floats to 6 places. A wrong answer shows the first case you got wrong, with what was expected. Your code gets 10 seconds, so an endless loop just fails.

Progress lives in ~/.local/share/sqlgym/progress.json and your answers in ~/.local/share/sqlgym/work/<NNN_slug>.sql (or .py); both respect $XDG_DATA_HOME.

Adding a problem

problems/<NNN_slug>/
  problem.toml   # title, difficulty, ordered (default false)
  prompt.md      # the question text
  setup.sql      # CREATE TABLE + INSERT statements
  solution.sql   # reference answer
  solution.py    # optional: the DataFrame answer `<leader>a` shows

A Python problem has problem.toml and prompt.md, then cases.py (a list of keyword arguments, cases = [dict(logs=[...]), ...]) and solution.py (a solve()) instead of the SQL files. The first case is shown as the example.

The schema and sample rows shown with each problem are generated from setup.sql. Run uv run pytest to check that every solution runs (and passes its cases).

Problems 001–099 come from pgexercises and are regenerated by uv run scripts/import_pgexercises.py, which writes scripts/import_report.md. Problems 101+ are original.

License

The code is MIT. Problems 001–099 are adapted from pgexercises by Alisdair Owens and stay under CC BY-SA 3.0. See LICENSE and problems/LICENSE.

About

LeetCode for SQL, in your terminal

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages