- 2 minutes read

Did you know SQL is Turing complete? Obviously, it's not. It's a purely declarative language. It's designed with a single purpose in mind: dealing with relational databases.

Surprisingly, modern SQL is Turing complete, indeed. This stunning claim has been proven by David Fetter. He's also written an example that really blew my hat off. It's possible to write a SELECT statement drawing a Mandelbrot set.

As I've mentioned before, SQL is a purely declarative language. So are HTML and XML. But there's no way HTML could be Turing complete, so this insight came as a surprise to me. The key to being Turing complete are the window functions offered by most modern SQL databases. These, in turn, allow for recursion, and that's one of the loopholes allowing a purely declarative language like SQL to sneak into the realm of procedural languages. That's the same trick that makes PROLOG a Turing complete language.

I got aware of the phenomenon when I attended the great JAX talk of Lukas Eder. Today, he's published a transcript of his talk, including the slides. Highly recommended!

Dig deeper

10 SQL Tricks That You Didn’t Think Were Possible by Lukas Eder

SQL Is Now Turing Complete

drawing a Mandelbrot set with PostgreSQL 8.4


Comments