← Back to context

Comment by TomasBM

2 days ago

Somewhat tangential to the article, but why is SQL considered a programming language?

I understand that's the convention according to the IEEE and Wikipedia [1], but the name itself - Structured Query Language - reveals that its purpose is limited by design. It's a computer language [2] for sure, but why programming?

[1] https://en.wikipedia.org/wiki/List_of_programming_languages

[2] https://en.wikipedia.org/wiki/Computer_language

"structured query language" is actually a backronym, SEQUEL is indeed a programming language and the only mainstream 4GL. consider the output of the compiler (query planner) is a program with specific behavior, just that your sql code is not the only source - the other inputs are the schema and its constraints and statistics. it's an elegant way to factor the sourcecode for a program, I wonder if Raymond Boyce didn't die young what kind of amazing technology we might have today.

With support for Common Table Expressions (CTE), SQL becomes a Turing complete language. To be honest, it makes me somewhat uncomfortable that a query sent to a DB server could be non-terminating and cause a server thread to enter an infinite loop. On the other hand, the practical difference between a query that contains an infinite loop and one that runs for days is probably negligible.

To be honest, I'd like to chip in that it is technically possible to write brainf*ck, an esoteric programming language but nonetheless, its a programming language

https://www.reddit.com/r/SQL/comments/81barp/i_implemented_a...

Btw this runs in sqlite, you can try it yourself if you are interested.

Source: I was thinking of creating a programming language paradigm like sqlite/smalltalk once where resumed execution/criu like possibilities were built in. Let me know if someone knows something like this too. I kinda gave up on the project but I knew that there was this one language which supported this paradigm but it was very complicated to understand and had a lot of other first time paradigm like the code itself / the ast tree is sort of like a database itself but so the tangential goes.

Because stored procedures do exist, and there isn't a single production quality RDMS that doesn't go beyond DDL and DML, adding structured programming extensions.

Also, even within the standard itself, it allows for declarative programming.

What is your definition of 'programming language'?

  • It should have arrays, and loops and conditionals.

    • OK. My definition is that it should be able to add two integers together and give you a result somehow. So in SQL:

          select 1+1; -- Result: 2
      

      In HTML: not possible.

      That's the key difference.

    • Slightly simplistic: table rows cover arrays, recursive CTEs cover loops, and JOIN/WHERE cover conditionals.

Because "programming language" is an adjective or a descriptive term. Whatever looks like a programming language, can be called a programming language.

Also SQL is not turing complete. I see it more as a descriptive language like e.g. html is a language but not a programming language.

  • This is completely wrong. The SQL spec isn't Turning complete but multiple DBs provide Turing complete extensions (like pgplsql) that make it so. Also, even without the extensions, it is still very much a programming language by any definition of the term. Like most takes on SQL, it is more about your understanding (or lack thereof) of SQL.

    • I was under the impression that recursive CTEs make the SQL spec Turing complete. Not that it makes any practical difference, it's still very difficult to use for "general purpose" programming, and still very practical to use for data processing / management.

      Last year I read about some database researcher who implemented AoC in pretty standard SQL.

    • If the spec isn't Turing complete, only individual extensions to the spec, I think it's correct to say "SQL isn't Turing complete".

  • It can do loops and recursion. It can use as much memory as it is allowed. It can do general programming via functions and stored procedures.