Chess in SQL
Comments
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.
The Board
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.
The Magic: Pivoting Rows into a Grid
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:
-
The CTE (WITH full_board AS ...) generates all 64 squares by cross-joining ranks 1-8 with files 1-8, then LEFT JOINs our pieces
-
The pivot uses MAX(CASE WHEN file = N THEN piece END) to extract each column's piece
-
COALESCE fills empty squares with · so we can see the grid structure
-
ORDER BY rank DESC puts rank 8 at the top (black's back rank), like a real board
Making Moves
Now 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.
Your Turn: The Sandbox
Here's a fully set up board. Try making some moves yourself. Some ideas:
-
Play the Italian Game: 1. e4 e5 2. Nf3 Nc6 3. Bc4
-
Try the Queen's Gambit: 1. d4 d5 2. c4
-
Set up a checkmate position
-
Or just mess around!
Remember:
-
Files: a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8
-
DELETE the piece from its starting square, INSERT it at the destination
-
For captures, DELETE both the moving piece AND the captured piece first
⚡
Loading SQL environment...
The Opera Game: A Chess Masterpiece in SQL
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.
Wrapping Up
We just built a fully playable chess board in pure SQL. No JavaScript. No frameworks. Just:
-
A simple table with rank, file, and piece
-
A clever pivot query using conditional aggregation
-
DELETE and INSERT to move pieces
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
Sign in to highlight and annotate this article

Conversation starters
Daily AI Digest
Get the top 5 AI stories delivered to your inbox every morning.
Knowledge Map
Connected Articles — Knowledge Graph
This article is connected to other articles through shared AI topics and tags.
More in Analyst News

PC enthusiast finds relic Nvidia 3D Vision 2 glasses for $2.99 — PC gaming artifact from 2011 cost $149 new, was once Nvidia's 'vision' for the future of gaming
PC enthusiast finds relic Nvidia 3D Vision 2 glasses for $2.99 — PC gaming artifact from 2011 cost $149 new, was once Nvidia's 'vision' for the future of gaming
Polish phonology and morphology through the lens of distributional semantics
arXiv:2604.00174v1 Announce Type: new Abstract: This study investigates the relationship between the phonological and morphological structure of Polish words and their meanings using Distributional Semantics. In the present analysis, we ask whether there is a relationship between the form properties of words containing consonant clusters and their meanings. Is the phonological and morphonological structure of complex words mirrored in semantic space? We address these questions for Polish, a language characterized by non-trivial morphology and an impressive inventory of morphologically-motivated consonant clusters. We use statistical and computational techniques, such as t-SNE, Linear Discriminant Analysis and Linear Discriminative Learning, and demonstrate that -- apart from encoding rich

Pay $611 for 64GB of Corsair Vengeance RAM and Samsung's 2TB PCIe Gen 5 9100 Pro when paired with a Gigabyte X870 Auros Elite motherboard and Corsair Frame 5000D case — matching colors for a pure white build
Pay $611 for 64GB of Corsair Vengeance RAM and Samsung's 2TB PCIe Gen 5 9100 Pro when paired with a Gigabyte X870 Auros Elite motherboard and Corsair Frame 5000D case — matching colors for a pure white build
COTTA: Context-Aware Transfer Adaptation for Trajectory Prediction in Autonomous Driving
arXiv:2604.00402v1 Announce Type: new Abstract: Developing robust models to accurately predict the trajectories of surrounding agents is fundamental to autonomous driving safety. However, most public datasets, such as the Waymo Open Motion Dataset and Argoverse, are collected in Western road environments and do not reflect the unique traffic patterns, infrastructure, and driving behaviors of other regions, including South Korea. This domain discrepancy leads to performance degradation when state-of-the-art models trained on Western data are deployed in different geographic contexts. In this work, we investigate the adaptability of Query-Centric Trajectory Prediction (QCNet) when transferred from U.S.-based data to Korean road environments. Using a Korean autonomous driving dataset, we comp
Discussion
Sign in to join the discussion
No comments yet — be the first to share your thoughts!