alt.hn

3/29/2026 at 1:25:16 PM

Chess in SQL

https://www.dbpro.app/blog/chess-in-pure-sql

by upmostly

3/29/2026 at 1:28:45 PM

Author here.

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.

by upmostly

4/2/2026 at 1:05:13 AM

One of the things that LLMs "excel" at, pun very much intended, is this exact pattern - creating filtered aggregates for a finite set of columns, and using this at the end of a CTE!

OP's example, for reference, was:

    SELECT rank,
    MAX(CASE WHEN file = 1 THEN COALESCE(piece, '·') END) AS a,
    MAX(CASE WHEN file = 2 THEN COALESCE(piece, '·') END) AS b,
    MAX(CASE WHEN file = 3 THEN COALESCE(piece, '·') END) AS c,
    MAX(CASE WHEN file = 4 THEN COALESCE(piece, '·') END) AS d,
This pattern is incredible for generating financial model drivers (where every column is a calendar/fiscal month/quarter/year, and every row is a different type of statistic/measure).

The broader pattern is, in successive CTEs:

1. Group by Date w/ Aggregates

2. "Melt" to [optional groupings +] month + measure_name + value tuples:

    select group, month, '# Bookings' as measure_name, num_bookings as value from base_data
    UNION ALL
    select group, month, 'Revenue', total_revenue from base_data
3. Then "pivot":

    MAX(CASE WHEN month = '2019-01' THEN value END) AS "2019-01",
    MAX(CASE WHEN month = '2019-02' THEN value END) AS "2019-02",
    MAX(CASE WHEN month = '2019-03' THEN value END) AS "2019-03",
And what you get is a full analysis table, with arbitrary groupings, that can be dropped into an Excel model in a way that makes life easy for business teams.

And while the column breakouts are painful to type out by hand - they're very amenable to LLM generation!

by btown

4/1/2026 at 8:35:09 AM

Great showcase. Cool to see how any 2d state can be presented with enough work.

Just FYI your statement for the checkmate state in the opera game appears to be incorrect

by jollygoodshow

4/1/2026 at 8:37:19 AM

Thank you, and thanks for highlighting that. I'll take a look now.

by upmostly

4/1/2026 at 7:34:07 AM

Technically you can model anything in SQL including execution of any Turing complete language

by andoando

4/1/2026 at 9:57:48 AM

Yes, but OP wants to preserve the relational goodness.

by eru

4/1/2026 at 10:03:18 PM

I've done huge sparse matrix math with SQL before. It parallelizes well like you'd expect.

by traderj0e

4/1/2026 at 6:40:33 AM

SQL can make 2D data, but it extremely bad at it. It’s a good opportunity to wonder whether this part can be improved.

“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.

by eastbound

4/1/2026 at 5:30:28 PM

> SQL can make 2D data, but it extremely bad at it. It’s a good opportunity to wonder whether this part can be improved.

R*Trees are what you are looking for. The sqlite implementation supports up to 5 dimensions.

by andersmurphy

4/1/2026 at 8:59:22 PM

in sqlite you can do it with FILTER:

   $ sqlite :memory: 
   create table t (product,revenue, year);
   insert into t values ('a',10,2020),('b',14,2020),('c',24,2020),('a',20,2021),('b',24,2021),('c',34,2021);
   select product,sum(revenue) filter (where year=2020) as '2020',sum(revenue) filter (where year=2021) as '2021' from t group by product;

by h3lp

4/1/2026 at 4:28:14 PM

Can you comment on whether you wrote the article yourself or used an LLM for it? To me it reads human (in a maybe slightly overly-punchy, LinkedIn-esque way), but a lot of folks are keying on the choppiness and exclusion chains and concluding it's AI-written.

I'm interested in whether others are oversensitive or I'm not sensitive enough... :)

by mwigdahl

4/1/2026 at 4:42:50 PM

They definitely used an LLM for it

by slopinthebag

4/1/2026 at 2:07:11 PM

Fascinating idea. Since the board starting position never changes, I'd skip the initial table and pivot and just go straight to loading an 8x8 grid with the pieces. I would also make a table of the 6 piece types and movement parameters. So, for ex, the bishop move restriction is dX=dY, the rook (dXdY=0), knight (dXdY=2), etc. Then a child table to record for each piece, the changes in X,Y throughout the game (so the current position of any piece is X = (Xstart + SUM(dX)) & Y = (Ystart + SUM(dY)) and a column to show if the piece was captured. Any proposed "move" (e.g., 3 squares up) would be evaluated against the move restrictions, the current location of the piece and whether or not the move will either land on an empty square, an opponent piece or gulp off the board and either allow or disallow it.

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.

by Beestie

4/1/2026 at 6:12:51 AM

You could take this even further and add triggers to see if your move is legal or not. Or delete row with a conflict when you capture a piece.

by eelinki

4/1/2026 at 3:55:44 PM

This is getting dangerously close to how some AAA MMORPGs handle[d] much of their logic and state management.

At the scales these games operate, enterprisey oracle clusters start to look like a pretty good solution if you don't already have some custom tech stack that perfectly solves the problem.

by bob1029

4/1/2026 at 4:57:25 PM

Interesting. I'm a big fan of MMORPGs so hearing that this is how they were made is really cool.

I've always wondered what kind of stack games like EverQuest were built on.

by upmostly

4/1/2026 at 5:59:11 PM

I started playing World of Warcraft at the same time I was studying database systems at university and had a similar curiosity. Twenty years later the AzerothCore project pretty much satisfies this curiosity, they've done an incredible job reverse engineering the game server and its database.

https://www.azerothcore.org/wiki/database-world

by ctippett

4/1/2026 at 6:47:27 PM

That’s fascinating. I didn’t realize the WoW server was so database heavy. do you know if the original game logic was implemented mostly in stored procedures, or was it just used for persistence and the engine handled the rules elsewhere?

by vakrdotme

4/1/2026 at 9:43:05 PM

It's not, no. The data you see in these files is reconstituted from the data that shipped with the game client, but they're not a perfect match for the real data.

The game servers are all C++ and don't use stored procedures for general gameplay management. They do handle inventory management so that item duping is generally not possible, and complex things like cross-server character transfer use stored procedures.

by StilesCrisis

4/1/2026 at 7:00:44 PM

I don't know I'm sorry. I'm not involved in the project, just a curious bystander!

by ctippett

4/1/2026 at 5:29:20 PM

Do you have a source? Curious to learn more about this

by davis

4/1/2026 at 8:04:27 PM

Impressive! Incidentally, I built my own Chess game from scratch pretty recently, using nothing but my own knowledge of the game rules and I am seeing some of the same patterns emerge, though I used plain data structures instead of tables. It’s always interesting to see different ways of solving the same problem, especially with inappropriate/inadequate tools. It’s kind of like figuring out how to make pizza without a proper oven.

by temporallobe

4/1/2026 at 10:13:28 AM

Nice post! It looks like the colors of the pieces are swapped though. Perhaps you could replace the dots with something else to indicate the colors of the individual squares too.

by antonautz

4/1/2026 at 8:12:34 AM

Nice. The trojan horse framing works well, once you see that any 2D state is just coordinates + a value, it’s hard to unsee it. Did you consider using this to enforce move legality via CHECK constraints or triggers, or did that get too hairy?

by danielszlaski

4/1/2026 at 6:43:46 AM

And they didn’t call it ChessQL?

by jimgoneill

4/1/2026 at 12:52:30 PM

This is such a cool way to build brand awareness - kudos to the author.

I'd never heard of dbpro.app until now - and this article is just so awesome.

Nice job!

by herodoturtle

4/1/2026 at 6:18:30 PM

Thank you! That means a lot.

by upmostly

4/1/2026 at 6:36:56 AM

Amazing, how do I play it?

by grimm8080

4/1/2026 at 8:47:30 PM

It's possible to code anything in SQL

by lovegrenoble

4/1/2026 at 8:55:30 AM

Of all the use cases for SQL chess would not have been on that list haha. Amazing.

by OfirMarom

4/1/2026 at 7:30:50 PM

Great idea. Appreciate your efforts

by PhishClean

4/1/2026 at 7:32:43 PM

doom in sql when? (real doom)

by dorianmariecom

4/1/2026 at 6:12:01 AM

Very cool! I think the dragon is missing a white rook - ascii chess pieces are heard to see...

by FergusArgyll

4/1/2026 at 11:30:32 AM

Very cool concept

by devlx

4/1/2026 at 3:28:59 PM

I had no idea SQL could do something like this lol

by hahooh

4/1/2026 at 4:00:03 PM

SQL can do anything as of recursive CTEs and application-defined functions with side-effects. Performance and ergonomics are the only remaining concerns.

by bob1029

4/1/2026 at 1:35:29 PM

very cool, you got the classic games as well :)

i once published a "translation" of the Opera Game (chess annotation as a literary device) after reading too much Lautremont so it is disgusting

by The_Blade

4/1/2026 at 5:58:45 PM

[dead]

by pagecalm

4/1/2026 at 3:41:27 PM

[dead]

by skrun_dev

4/1/2026 at 8:11:07 AM

> No JavaScript. No frameworks. Just SQL.

> 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

by catlifeonmars

4/1/2026 at 1:45:17 PM

The web was already not doing so well, but now I fear LLMs will be the final blow for me. This sort of thing is just unreadable. I don't see how people put up with it. Well, maybe not "people". This thread is full of new accounts saying "cool!". Unfortunately I think HN is on its way out.

by mahogany

4/1/2026 at 12:37:04 PM

Yeah, I wish this would have just been a short human-written post of a few paragraphs. The emdashes, bulletpoints and unnecessary dramatic flourishes really detract from it.

> 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.

by streetfighter64

4/1/2026 at 6:12:04 AM

Tool looks nice, but I would prefer such a tool written in a better (native?) language than JavaScript. Security is also important to me, so I only use open-source tools. I’m going to stick with DBeaver and DataGrip.

by landsman