Yes, SQL is a programming language: a declarative, domain-specific one for working with data in relational databases. You describe the rows you want, and the database decides how to fetch them. Plain SQL has no general-purpose features such as user interfaces or file handling, and procedural extensions like T-SQL and PL/pgSQL add loops and variables.
What "programming language" means here
People asking this usually mean one of two things. Either "does SQL count as real coding?" or "can SQL do what Python or JavaScript do?". The first answer is yes. The second is no, and the difference is about design, not quality.
SQL is a domain-specific language. It was built for one job: reading and changing data stored in tables. It is also declarative. Compare these two ways of getting the three cheapest products.
In a general-purpose language you write the steps. Example in Python:
cheapest = sorted(products, key=lambda p: p["price"])[:3]
In SQL you state the result:
SELECT name, price
FROM products
ORDER BY price
LIMIT 3;
Nothing in the SQL says how to sort, whether to use an index, or in what order to read rows. The PostgreSQL documentation puts it this way: the server "devises a query plan for each query it receives", and "the system includes a complex planner that tries to choose good plans". The same query can run as a full table scan on one data set and an index lookup on another, with no change to your text. You can inspect that choice with EXPLAIN.
What plain SQL has and lacks
Standard SQL statements are individual commands. The PostgreSQL docs describe it directly: "every SQL statement must be executed individually by the database server." Each statement is sent, run and answered on its own, and your own code decides what to send next.
| Feature | Plain SQL queries | Procedural extensions |
|---|---|---|
| Describe a result set | Yes (SELECT, JOIN, GROUP BY) | Yes |
| Change data | Yes (INSERT, UPDATE, DELETE) | Yes |
| Local variables | No | Yes (DECLARE @x in T-SQL) |
| Loops | Only through recursive queries | Yes (WHILE in T-SQL) |
| Build a user interface, serve web pages | No | No |
Where SQL does act like a general-purpose language
Procedural extensions
Most database products ship a procedural layer. PostgreSQL has PL/pgSQL, which its documentation describes as a loadable procedural language that "adds control structures to the SQL language" and can be used to "create functions, procedures, and triggers". Its stated purpose is to group "a block of computation and a series of queries inside the database server", which removes extra client and server round trips.
Microsoft's Transact-SQL (T-SQL), used by SQL Server and Azure SQL, has these features built in. Variables are "declared in the body of a batch or procedure with the DECLARE statement and are assigned values by using either a SET or SELECT statement". A WHILE statement "sets a condition for the repeated execution of a SQL statement or statement block", with BREAK and CONTINUE to control the loop. A small example in T-SQL:
DECLARE @counter INT = 1;
WHILE @counter <= 3
BEGIN
PRINT 'Pass ' + CAST(@counter AS VARCHAR(10));
SET @counter = @counter + 1;
END;
Example output: Pass 1, Pass 2, Pass 3, one line each. This uses a variable, a condition and a loop, which is what most people mean by "programming".
Recursive queries
Even without a procedural layer, SQL has WITH RECURSIVE. The PostgreSQL docs say this modifier "changes WITH from a mere syntactic convenience into a feature that accomplishes things not otherwise possible in standard SQL". Their example adds the integers 1 to 100:
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t;
The query returns 5050. The docs note that such queries are "evaluated iteratively", and warn that if the recursive part never stops returning rows, "the query will loop indefinitely". So SQL queries can contain a loop with a stopping condition, and an infinite loop is a real mistake you can make.
SQL compared with Python or JavaScript
- Style. SQL is declarative: you describe the result. Python and JavaScript are imperative: you give the steps.
- Scope. SQL works on data in a database. Python and JavaScript can read files, call web services, draw interfaces and run servers.
- Where it runs. SQL runs inside a database engine. Applications usually send SQL as text to the engine and receive rows back.
- Dialects. The core is shared across products, but extensions differ. T-SQL, PL/pgSQL and Oracle's PL/SQL are not interchangeable, and some PostgreSQL features are, in its own words, "extensions to the standard". Check your database's documentation before copying a snippet.
Because applications build SQL text from program values, they have to pass those values safely. See SQL injection explained for what goes wrong when they don't.
If someone says SQL isn't "real" programming
The claim usually rests on one of two points. The first is that SQL is not general-purpose, which is true and does not change its status as a language. The second is that SQL is "just queries". Once you add procedures, triggers, variables and loops, as T-SQL and PL/pgSQL do, that argument weakens, though the declarative core is still how most SQL gets written.
A practical test: if you can express logic, test it, and get a wrong answer from a mistake in it, you are programming. SQL meets that test. If you are new to the vocabulary, what a variable is in programming explains one concept SQL borrows once you use a procedural extension.
Frequently asked questions
- Is SQL a programming language or a query language?
- Both descriptions are used. SQL is a query language for relational databases, and a query language is a kind of programming language: you write statements that a machine executes. It is declarative, so it differs from languages such as Python or Java, where you spell out the steps.
- Is SQL Turing complete?
- Standard SQL queries alone are not usually described that way, but the PostgreSQL documentation says the RECURSIVE modifier on WITH 'accomplishes things not otherwise possible in standard SQL'. Procedural extensions such as T-SQL and PL/pgSQL add loops and variables on top. Whether a particular dialect is Turing complete depends on which features you count.
- Can you build a whole application in SQL?
- In practice, no. SQL has no built-in way to draw a user interface or handle web requests. Applications are usually written in another language, which sends SQL statements to the database. Stored procedures can hold a lot of logic inside the database, but they still run in a database server.
- Should I learn SQL before Python or JavaScript?
- Neither order is required. SQL is smaller than a general-purpose language and gives quick results on real data, so many people start there. The concepts of tables, rows and filtering transfer to any language that talks to a database.