vikrant69g blog

Someone built a neural network entirely in SQL

A developer trained MNIST digit recognition using only SQL queries. No Python. No frameworks. Just recursive CTEs and window functions.

Code snippet showing SQL queries implementing neural network operations with matrix multiplications and backpropagation logic

Someone on Hacker News implemented a working neural network using only SQL. Not a SQL wrapper around PyTorch. Not stored procedures calling out to Python. Pure SQL queries doing forward and backward propagation. The demo trains on MNIST, the handwritten digit dataset. Matrix multiplications become joins and aggregations. Activation functions are CASE statements. Backpropagation is recursive CTEs updating weight tables. It converges. Slowly, but it converges. This sits in the weird category of things that work but nobody would deploy. SQL was not designed for gradient descent. There is no autodiff. Every derivative is hand-coded. The query planner is optimising for throughput, not GPU-style parallelism. But it proves the constraint. If you can express a computation in relational algebra, you can train a model. Databases already handle tensors as arrays, aggregations as reductions, and loops as recursion. The primitives exist. They are just inefficient for this use case. The practical takeaway is not “build ML in SQL”. It is that SQL is more expressive than people assume. If you are stuck on a problem and the only tool available is a database, you have more options than you think. Probably still the wrong options, but options.


Source: Show HN: I implemented a neural network in SQL