Skip to main content

Databases

What is SQL?

Definition

SQL is the standard declarative language for querying and modifying data in relational databases: you describe the result you want, not how to retrieve it.

Also known as: Structured Query Language

SQL in more detail

You write SELECT with the columns, tables, joins and conditions that define the answer, and the database's query planner decides the strategy — which index to use, which table to scan, in which order to join. That separation is why the same query keeps working after an index is added or the data grows a hundredfold, and why understanding the planner's output matters more than micro-optimising the text.

The language divides into parts worth naming: DML for the data itself (SELECT, INSERT, UPDATE, DELETE), DDL for structure (CREATE, ALTER, DROP), DCL for permissions (GRANT, REVOKE) and TCL for transactions (BEGIN, COMMIT, ROLLBACK). Modern SQL is far richer than the basics suggest — window functions, common table expressions, recursive queries, JSON operators and upserts remove whole categories of application code.

It is standardised and yet not portable. Every engine implements a dialect: string concatenation, date arithmetic, pagination syntax, upsert, JSON functions and identifier quoting all differ between PostgreSQL, MySQL, SQL Server, Oracle and SQLite. Two habits are non-negotiable regardless of dialect — parameterise every value that comes from a user, because string concatenation is exactly how SQL injection happens, and check the query plan before assuming a query is fast.

SQL example

-- Join, aggregate, filter the aggregate
SELECT u.id,
       u.email,
       COUNT(o.id)      AS orders,
       SUM(o.total)     AS lifetime_value
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE o.created_at >= now() - interval '90 days'
GROUP BY u.id, u.email
HAVING COUNT(o.id) > 3
ORDER BY lifetime_value DESC
LIMIT 20;

-- Window function: rank within each group, no self-join
SELECT user_id, total,
       ROW_NUMBER() OVER (PARTITION BY user_id
                          ORDER BY total DESC) AS rn
FROM orders;

-- Always parameterise user input
SELECT * FROM users WHERE email = $1;   -- never string concat

-- See what the planner will actually do
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 12;

SQL — common questions

Is SQL a programming language?
It is a declarative query language, and a Turing-complete one once recursive CTEs are counted. It is not a general-purpose language: you state the result you want rather than the control flow to produce it.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation. Conditions on aggregate results such as COUNT(*) > 3 must go in HAVING, because the aggregate does not exist yet when WHERE runs.
Is SQL portable between databases?
The core is, and the details are not. Pagination, upserts, date functions, JSON handling and string operations vary between engines, so any non-trivial application accumulates dialect-specific SQL.

Keep reading

This page explains what SQL means. For the syntax itself, the SQL cheat sheet is a full reference of real commands and examples. Every other definition is in the A-Z glossary.

Browse the A-Z glossary