I had the idea of building a working Chess game using purely SQL.
The chess framing is a bit of a trojan horse, honestly. The actual point is that SQL can represent any stateful 2D grid. Calendars, heatmaps, seating plans, game of life. The schema is always the same: two coordinate columns and a value. The pivot query doesn't change.
A few people have asked why not just use a 64-char string or an array type. You could! But you lose all the relational goodness: joins, aggregations, filtering by piece type. SELECT COUNT(*) FROM board WHERE piece = '♙' just works.
“Pivot tables”: I often have a list of dates, then categories that I want to become columns. SQL can’t do that so there is a technique of spreading values to each column then doing a MAX of each value per date. It is clumsy and verbose but works perfectly… as long as categories are known in advance and fixed. There should be an SQL instruction to pivot those rows into columns.
Example: SELECT date, category, metric; -- I want to show 1 row per date only, with each category as a column.
``` SELECT date,
MAX( CASE category WHEN ‘page_hits’ THEN metric END ) as “Page Hits”,
MAX( CASE category WHEN ‘user_count’ THEN metric END ) as “User Count”
GROUP BY date;
^ Without MAX and GROUP BY: 2026-03-30 Value1 NULL 2026-03-30 NULL Value2 2026-03-31 Value1 NULL (etc) The MAX just merges all rows of the same date. ```
SQL should just have an instruction like: SELECT date, PIVOT(category, metric); to display as many columns as categories.
This thought should be extended for more than 2 dimensions.
I'm still working on an idea to have a "state" check to know when checkmate happens but that's gonna take a wee bit more time.
But, the idea is very novel and very thought provoking and has provided me with a refreshing distraction from the boring problem I was working on before seeing your post.
I'd never heard of dbpro.app until now - and this article is just so awesome.
Nice job!
i once published a "translation" of the Opera Game (chess annotation as a literary device) after reading too much Lautremont so it is disgusting
> Let's build it.
Cool concept; but every blog post sounds exactly the same nowadays. I mean it’s like they are all written by the exact same person /s
> Not "store chess moves in a database." Not "track game state in a table." Actually render a chess board. With pieces. That you can move around. In your browser. Using nothing but SELECT, UPDATE, and a bit of creative thinking.
Please, just write like a person.
What if I told you SQL could play chess?
Not "store chess moves in a database." Not "track game state in a table." Actually render a chess board. With pieces. That you can move around. In your browser. Using nothing but SELECT, UPDATE, and a bit of creative thinking.
Loading chess board...
No JavaScript. No frameworks. Just SQL.
Let's build it.
First, we need to represent the chess board. A chess board is an 8x8 grid. Each square can either be empty or contain a piece. That's just a table:
⚡
Loading SQL environment...
We've got 32 rows - one for each piece on the starting board. But that's not very... chess-like. We want to see an actual board.
Here's where it gets interesting. SQL doesn't naturally output grids - it outputs rows. But we can transform rows into columns using a technique called conditional aggregation.
The idea: GROUP BY the rank (row), and for each file (column), use a CASE statement inside MAX() to pick out the piece:
⚡
Loading SQL environment...
There it is. A chess board. Rendered entirely in SQL.
Let's break down what's happening:
WITH full_board AS ...) generates all 64 squares by cross-joining ranks 1-8 with files 1-8, then LEFT JOINs our piecesMAX(CASE WHEN file = N THEN piece END) to extract each column's piece· so we can see the grid structureNow for the fun part. To move a piece, we just UPDATE the board:
⚡
Loading SQL environment...
Both pawns have advanced! The most common chess opening, executed in pure SQL.
Here's a fully set up board. Try making some moves yourself. Some ideas:
Remember:
⚡
Loading SQL environment...
Let's replay one of the most famous chess games ever played. In 1858, Paul Morphy played against the Duke of Brunswick and Count Isouard at the Paris Opera (during a performance of The Barber of Seville, no less).
It's a beautiful demonstration of rapid development and tactical brilliance. Let's watch it unfold in SQL.
Morphy has developed his pieces aggressively, targeting the weak f7 pawn:
⚡
Loading SQL environment...
Morphy sacrificed his bishop, but now his knight joins the attack with devastating effect:
⚡
Loading SQL environment...
The finale is stunning. Morphy plays Rd8+, and when the Queen takes the rook, the other rook delivers checkmate:
⚡
Loading SQL environment...
The white rook on d8 delivers checkmate. The bishop on f8 blocks the king's escape, and the knight on b5 covers d6. A masterpiece then, a masterpiece now - rendered in SQL.
We just built a fully playable chess board in pure SQL. No JavaScript. No frameworks. Just:
The same pivot technique works for any grid-based visualization - calendars, seating charts, game boards, heatmaps. SQL is more expressive than most people give it credit for.
Now if you'll excuse me, I have a rematch against a database to prepare for.
Jay