Oh, the question that stumps novice coders and veterans alike; is SQL really a programming language? Well, there is actually a pretty clear answer; albeit, a tricky one. The short answer is yes, SQL is a programming language. However, it is a lot more tricky to answer that question once we get into specifics.
Below we are going to walk through exactly what SQL is, and what it is not. By the end of this article, you will have a good understanding of what SQL is, and why this confusion arises.
So without further ado, let’s jump right in.
Is SQL a Programming Language?
First, let’s explain why SQL actually is a programming language.
A programming language is a vocabulary and set of grammatical rules for instructing a computer or computing device to perform specific tasks. From this definition, we can rest assured that SQL is a programming language. In SQL we use a set of grammatical rules and vocabulary in order to instruct a database to perform a specific task.
Let’s give a specific example to make this a bit clearer:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
Within our example you can see we are using a vocabulary and set of grammatical rules INSERT INTO
and VALUES
in order to instruct the computer to store the values into the database. We are still instructing the computer to do the work, and the specific task is to alter the database.
From this example, we can be 100% sure that SQL definitely is a programming language. However, what if we want SQL to do more than be the language to communicate with a database?
Is SQL A General Purpose Programming Language?
This is where the biggest confusion is seen in the debate between SQL and whether or not it is a programming language. When most people think of programming languages, they think of general-purpose programming languages like Python, C, JavaScript, or PHP. These languages may have their places in specific domains but they are versatile enough to be used in a very wide range of applications and uses.
This is what a general-purpose programming language is and how it gets its name. It is useful in general, not just in a single job or domain. SQL however, is really only useful when used to communicate with a database. For that reason, it cannot be considered a general-purpose programming language. You can’t use SQL to create a web application or to program your microwave!
Is SQL A Domain-Specific Programming Language?
So if SQL isn’t a general-purpose programming language, what is it? Well, SQL is actually a domain-specific programming language. A language that is used for a single purpose or domain.
A very popular example of a domain-specific language is both HTML and CSS. HTML is used to construct data onto a webpage and CSS is used to style it. Both of these languages have very specific uses. Just as SQL has a very specific use to manipulate databases.
Why create domain-specific languages and not just use the same language for everything? Well, because these languages generally do a much better job at one thing than attempting to be a ‘jack-of-all-trades’, like most popular languages.
For instance, SQL is extremely quick at querying databases and making changes. If you needed to update 1 million rows in a table, you could do so quicker with SQL than with any other language. Simply because SQL was designed to be extremely efficient in this domain.
Programming Language vs. General-Purpose Programming Language
Is SQL Turing Complete?
Some people like to test whether or not a language is indeed a programming language based on whether or not it is Turing Complete. If you do not, know what Turing Completeness is, it is essentially a test to see if the language in question has a set of rules that allow it to solve any problem a machine can solve.
In other words, if a language is Turing Complete, it should, in theory, be able to solve any problem that has been solved by a computer before. For more info, check out this explain like I’m 5 link.
As it turns out, yes SQL is Turing Complete. Even if you exclude the PL/SQL or PSM languages (more on these later).
This was proved by Andrew Gierth, when he created a Cyclical Tag System by using CTE and Windowing SQL. Since Cyclical Tag Systems are Turing Complete, we can conclude that SQL is also Turing Complete.
Is SQL a Scripting Language?
To know whether or not SQL is a scripting language or not, we need to know what a scripting language is. Simply put, a scripting language is a programming language that executes tasks within a special run-time environment by an interpreter instead of a compiler.
As you might have guessed, yes, that means SQL is a scripting language. As the language does not get compiled down such as a language like C. Instead, the language is interpreted and passed into the database engine from applications and other clients as text not as a compiled code stream.
SQL and Procedural Languages?
SQL itself is not a procedural programming language. However, there are extensions that developers have created to SQL that have made it a procedural programming language. For instance, PL/SQL by Oracle and SQL/PSM are both procedural programming languages. So while we cannot use SQL as a procedural language, there are extensions that do exist to make it one.
What is MySQL? Is MySQL A Programming Language?
Finally, I wanted to address this last question, as I hear it a lot. No, MySQL is not a programming language. MySQL is simply a database management system. You do use SQL within MySQL, however, MySQL itself is not a programming language nor a language at all.
Summary
Hopefully, at the end of this article, you understand exactly what kind of language SQL is and is not. Yes, SQL is a programming language and is Turing Complete. It can also be classified as a domain-specific language and a scripting language. However, SQL certainly is not a general-purpose language and can only be considered a procedural language through its extensions, such as PL/SQL and SQL/PSM.
If you are new to development and interested in learning SQL, check out this amazing resource.
If you’re new to web development and want to know everything you need to know, check out this free resource.