Chess in Pure SQL: The Most Unexpected Chess Board You'll Play On
♟️ Read the original post · by Jay at DB Pro
I have seen a lot of weird chess implementations. Chess in Minecraft redstone. Chess in Excel. Chess in Conway’s Game of Life. Chess in JavaScript frameworks that should not exist.
But this one got me.
Jay at DB Pro built a fully playable chess board in pure SQL. Not “store moves in a database.” Not “track game state.” Actually render a board with pieces you can move around in your browser, using nothing but SELECT, UPDATE, DELETE, and INSERT.
How It Works
The board is a table. 32 rows, one for each piece on the starting position. Each row has a rank, a file, and a piece identifier. Simple enough.
The magic is in the rendering. SQL outputs rows, not grids. But you can pivot rows into columns using conditional aggregation — MAX(CASE WHEN file = N THEN piece END) grouped by rank. Suddenly your flat table becomes an 8x8 grid with pieces in the right places. COALESCE fills empty squares with · so you can see the structure.
To move a piece, you UPDATE its rank and file. Pawn to e4? UPDATE pieces SET file = 5, rank = 4 WHERE piece = 'P' AND file = 5 AND rank = 2. That is the entire move.
The Opera Game
The best part is they replayed Paul Morphy’s famous 1858 Opera Game. All of it. The bishop sacrifice on f7. The knight joining the attack. The final rook checkmate. Every move expressed as SQL statements against the board table.
Morphy played that game during a performance of The Barber of Seville at the Paris Opera. Now you can replay it in a terminal connected to PostgreSQL. Something about that feels right.
Why This Matters
It is a reminder that SQL is more expressive than people give it credit for. We spend so much time treating databases as dumb storage — insert, select, update, delete, go home. But the same pivot technique that renders a chess board works for calendars, seating charts, heatmaps, anything grid-based.
Also, it is funny. A chess board rendered entirely in a language designed for querying relational data. No JavaScript, no HTML, no CSS. Just SQL and a browser that can display the result.
The original post has an interactive sandbox so you can try making moves yourself. I spent way too long playing the Italian Game against a database table.
Chess in SQL: not the most practical thing I have seen this year. Definitely the most entertaining.
Crepi il lupo! 🐺