NHacker Next
login
▲Chess in SQLdbpro.app
140 points by upmostly 3 days ago | 28 comments
Loading comments...
upmostly 3 days ago [-]
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.

mwigdahl 15 minutes ago [-]
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... :)

jollygoodshow 8 hours ago [-]
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

upmostly 8 hours ago [-]
Thank you, and thanks for highlighting that. I'll take a look now.
andoando 9 hours ago [-]
Technically you can model anything in SQL including execution of any Turing complete language
eru 7 hours ago [-]
Yes, but OP wants to preserve the relational goodness.
eastbound 10 hours ago [-]
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.

tn1 9 hours ago [-]
DuckDB and Microsoft Access (!) have a PIVOT keyword (possibly others too). The latter is of course limited but the former is pretty robust - I've been able to use it for all I've needed.
amichal 5 hours ago [-]
PostgresSQL

"crosstab ( source_sql text, category_sql text ) → setof record"

https://www.postgresql.org/docs/current/tablefunc.html

VIA https://www.beekeeperstudio.io/blog/how-to-pivot-in-postgres... as a current googlable reference/guide

Beestie 3 hours ago [-]
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.

eelinki 11 hours ago [-]
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.
bob1029 47 minutes ago [-]
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.

antonautz 6 hours ago [-]
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.
jimgoneill 10 hours ago [-]
And they didn’t call it ChessQL?
upmostly 8 hours ago [-]
I thought about it, but, not surprisingly, that already exists.

https://pypi.org/project/chessql/

danielszlaski 9 hours ago [-]
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?
herodoturtle 4 hours ago [-]
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!

grimm8080 10 hours ago [-]
Amazing, how do I play it?
hahooh 1 hours ago [-]
I had no idea SQL could do something like this lol
bob1029 43 minutes ago [-]
SQL can do anything as of recursive CTEs and application-defined functions with side-effects. Performance and ergonomics are the only remaining concerns.
landsman 11 hours ago [-]
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.
OfirMarom 8 hours ago [-]
Of all the use cases for SQL chess would not have been on that list haha. Amazing.
devlx 5 hours ago [-]
Very cool concept
FergusArgyll 11 hours ago [-]
Very cool! I think the dragon is missing a white rook - ascii chess pieces are heard to see...
The_Blade 3 hours ago [-]
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

skrun_dev 1 hours ago [-]
[dead]
catlifeonmars 9 hours ago [-]
> 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

mahogany 3 hours ago [-]
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.
streetfighter64 4 hours ago [-]
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.