SQL
The standard language for defining, querying and changing data in relational databases
The standard language for defining, querying and changing data in relational databases
SQL (Structured Query Language) is the standard language for defining, querying and changing data held in relational databases, where information is kept in tables of rows and columns.

Via Wikimedia Commons: Relational model concepts by User:AutumnSnow, CC BY-SA 3.0.
The American national standard that first fixed the language describes it as specifying "the syntax and semantics of interfaces to a database management system for defining and accessing SQL databases". It was designed at IBM in the 1970s and became an American standard in 1986.
SQL grew out of a theory. In 1970 Edgar F. Codd, a mathematician at IBM's San Jose Research Laboratory, published "A Relational Model of Data for Large Shared Data Banks", which proposed a way to store and access information in large databases without imposing a fixed organizational structure on it or recording the exact location of each item. Codd received the Turing Award from the Association for Computing Machinery in 1981, according to IBM.
Codd's ideas still needed a language that people could use. Donald Chamberlin, then at IBM's laboratory in Yorktown Heights, New York, recalled at a 1995 reunion of the researchers involved that a seminar Codd gave there was "a conversion experience" that made him understand what the relational approach was about. Queries that would have taken pages of program code in the database systems of the day, Codd wrote down as one-liners, such as "Find the employees who earn more than their managers." Chamberlin and his new colleague Raymond Boyce then played what they called the Query Game, looking for ways to express complicated queries, and devised a notation called SQUARE, short for "Specifying Queries as Relational Expressions".
In 1973 a group at IBM began System R, an industrial-strength implementation of the relational model. Chamberlin and Boyce worked on its language. SQUARE, Chamberlin explained at the reunion, could not be typed on a keyboard because it used subscripts, so they recast its ideas as English keywords and called the result Structured English Query Language, or SEQUEL. They added features, such as GROUP BY, that had no counterpart in SQUARE, and built a prototype on top of an access method written by Raymond Lorie.
Chamberlin and Boyce presented the language in a paper, "SEQUEL: A Structured English Query Language", at an ACM workshop on data description, access and control held in Ann Arbor, Michigan, in May 1974. Its abstract describes a set of simple operations on tables that could be used "without resorting to the concepts of bound variables and quantifiers", the notation of formal logic, and says the language was meant both for professional programmers and for occasional users of a database.
Boyce did not see the language grow. In the spring of 1974 he collapsed at lunchtime from a brain aneurysm, and he died on Father's Day 1974, Chamberlin recalled. Their collaboration had been so close, Chamberlin said, that at the end of a day neither could remember which ideas had been whose.
The designers' original aim was to let people who were not programmers question a database directly. Looking back in 1995, Chamberlin judged that SQL had instead succeeded in other ways, among them as a common medium between different systems, rather than as a language for occasional users: "the problem that we solved wasn't really the problem that we thought we were solving at the time."
The name changed after a legal challenge. Chamberlin recalled that a company, which he believed was the British aircraft maker Hawker Siddeley, claimed SEQUEL as its registered trademark, and that he himself removed the vowels to make SQL, following the pattern of three-letter language names ending in L such as APL.
The language reached the market from more than one direction. At the 1995 reunion Roger Bamford, a former IBM engineer who had joined Oracle, said that "IBM invented the language, but Oracle shipped it first." Jim Gray suggested the year was 1979, and Bamford noted that the first release was numbered Version 2. Others at the reunion said that Oracle's code did not come from System R, whose code its developers would not have had access to. IBM's own history credits Larry Ellison's company Relational Software, later renamed Oracle, with the first commercially available relational database.
IBM's own database product DB2 was first shipped in 1983 on the MVS mainframe operating system. IBM also credits Patricia Selinger with developing a cost-based optimizer, the part of the system that chooses an efficient way to carry out a query, and Raymond Lorie with a compiler that could save the plans for queries to be used again.
The American National Standards Institute approved American National Standard X3.135-1986, Database Language SQL, on 16 October 1986. It was developed by the Technical Committee on Database, X3H2, whose project, as quoted in the standard's foreword, was to standardize the functions "used in defining, querying, and altering relational databases". The United States government adopted it for federal use as Federal Information Processing Standards Publication 127.
At the 1995 reunion Chamberlin recalled that the first standard grew from a precise written description of what System R did, prepared by Bob Engles, which the standards committee then adopted: "They kept all the warts, too." Jim Gray recalled that the international standards body accepted the American text in return for the addition of its own design for referential integrity, the foreign keys that tie a row in one table to a row in another.
The international standard is ISO/IEC 9075. It is revised from time to time, and each version replaces the one before it. As of November 2026 the latest is SQL:2023; earlier versions were SQL:2016, SQL:2011, SQL:2008, SQL:2006, SQL:2003, SQL:1999 and SQL-92. The standard is divided into parts: Part 1 sets out the framework, Part 2, called SQL/Foundation, defines the core of the language, and others cover subjects such as calling SQL from other programming languages, XML, multi-dimensional arrays and, in Part 16, queries over property graphs.
Conformance to the standard is partial in practice. The PostgreSQL documentation notes that most systems claiming conformance to SQL-92 met only its lowest, Entry level, and that at the time of writing no current database system claimed full conformance to Core SQL:2023, the set of features every conforming implementation must supply. It reports that PostgreSQL conforms to at least 170 of the 177 mandatory Core features.
A query in SQL describes the result that is wanted, and the database system works out the steps for producing it; IBM's history credits Chamberlin and Boyce with developing "systems for automatically translating high-level queries". A query has a characteristic shape. In the PostgreSQL documentation's terms, a table expression has a FROM clause, naming the tables to read, optionally followed by WHERE, GROUP BY and HAVING clauses that act as a pipeline of successive transformations on the rows FROM produces. A SELECT list then names the columns or computed values to return. For example:
SELECT department, COUNT(*)
FROM employees
WHERE salary > 50000
GROUP BY department;
asks, for each department, how many employees earn more than 50,000.
A frequent surprise follows from how joins are defined. In an inner join, for each row of the first table the result contains a row for each row in the second table that satisfies the join condition. If one customer has three orders and two addresses, joining customers to both tables yields six rows for that customer, so a SUM taken over the joined rows can count the same order more than once. Listing several tables in FROM with no join condition at all forms their Cartesian product: tables of N and M rows produce N × M rows.

Via Wikimedia Commons: Sql statement anatomy by SqlPac, CC BY-SA 3.0.
Before rows can be queried, the tables that hold them must be declared, with a name and a data type for each column. Rules called constraints can be attached to columns so that the database itself refuses data that breaks them. The PostgreSQL documentation describes the main kinds:
CHECK (price > 0).A short declaration using several of them:
CREATE TABLE orders (
order_id integer PRIMARY KEY,
product_no integer REFERENCES products (product_no),
quantity integer NOT NULL CHECK (quantity > 0)
);
SQL represents a missing or unknown value with the marker NULL, and much of its reputation for surprising behaviour comes from it. SQL uses a three-valued logic of true, false and null, where null means "unknown". So TRUE AND NULL is null, while TRUE OR NULL is true; FALSE AND NULL is false, while FALSE OR NULL is null.
Ordinary comparisons follow the same rule. A comparison with null yields null rather than true or false: 7 = NULL is null, and so is NULL = NULL, because it is not known whether two unknown values are equal. A condition such as WHERE column = NULL therefore never selects any rows; the test for a missing value is written IS NULL. The predicates IS DISTINCT FROM and IS NOT DISTINCT FROM compare values while treating two nulls as equal to each other. PostgreSQL offers a setting that rewrites x = NULL as x IS NULL for older applications, while recommending that such applications be changed to follow the standard.
The same logic catches out subqueries. When a list produced by a subquery contains a null and the value being tested matches nothing in it, x NOT IN (subquery) gives null rather than true, so a filter written with NOT IN can return no rows at all. EXISTS behaves differently: it asks only whether the subquery returns any rows, and its result is always true or false.
Aggregate functions treat nulls in their own way. In PostgreSQL, COUNT(*) counts every input row, while COUNT(column) counts only the rows in which that column is not null, so the two can give different totals for the same rows.
Even database builders have found the rules hard to pin down. The developers of SQLite, describing how they settled their own system's treatment of nulls, wrote that the standard's descriptions "seem ambiguous", so instead of relying on the documents they had volunteers run a test script on several popular database engines and made SQLite behave like the others.
SQL also has ways to write longer analyses as single statements. The WITH clause lets a query define named auxiliary statements, usually called common table expressions, which the PostgreSQL documentation compares to temporary tables that exist just for one query. Mostly this is a convenience that replaces nested subqueries with named steps. With the RECURSIVE modifier, however, a WITH query can refer to its own output, which the documentation says makes possible things not otherwise possible in standard SQL. Such recursive queries are typically used for hierarchical or tree-structured data, for example to find all the direct and indirect sub-parts of a product from a table that records only which part goes directly into which.
A window function performs a calculation across a set of rows related to the current row. It resembles an aggregate such as AVG, but it does not collapse the rows into a single output row: each row keeps its identity and gains the computed value beside it. The OVER clause after the function's name is what marks it as a window function, and PARTITION BY inside it divides the rows into groups. For example:
SELECT depname, empno, salary,
avg(salary) OVER (PARTITION BY depname)
FROM empsalary;
lists every employee alongside the average salary of that employee's department.
SQL groups changes into transactions. A transaction bundles several steps into a single, all-or-nothing operation: if a failure stops it partway, none of its steps affect the database, and its intermediate states are not visible to other transactions running at the same time. The PostgreSQL tutorial illustrates this with a payment of $100.00 from one bank account to another, which takes several UPDATE statements that must all happen or none. The statements are placed between BEGIN and COMMIT; issuing ROLLBACK instead of COMMIT cancels every change made so far. A SAVEPOINT marks a point inside a transaction that it can roll back to while keeping the work done before it. PostgreSQL treats every statement as part of a transaction, wrapping a lone statement in an implicit BEGIN and COMMIT.
Because many users work on the same data at once, the standard also defines how far concurrent transactions may see each other's work. It names four isolation levels: Read Uncommitted, Read Committed, Repeatable Read and Serializable. The strictest, Serializable, requires that running a set of transactions at once produce the same effect as running them one at a time in some order. The weaker levels are defined by which anomalies they forbid:
The standard sets only the minimum protection at each level, and systems may give more. PostgreSQL, for instance, accepts all four level names but implements three: its Read Uncommitted behaves like Read Committed, which is its default, and its Repeatable Read also prevents phantom reads.
Because SQL statements are text, a program that builds a statement by pasting in text supplied by a user can be tricked into running commands its author never intended. This weakness, SQL injection, is catalogued by MITRE as CWE-89, which describes a product that builds all or part of an SQL command from outside input without neutralizing special elements that could modify the intended command. The OWASP Foundation's prevention guide says an attack occurs when untrusted data becomes part of a query's SQL syntax, modifying the logic of the request. Its first defence is the parameterized query: the structure of the statement is defined separately and the values are bound to it through the database interface, so input is always treated as data. The guide warns that pasting untrusted values into the text before preparing it defeats this protection, and that names of tables and columns, which cannot be passed as parameters, should be chosen from a fixed list of allowed values.
Database systems depart from the standard in places, and the systems themselves document the gap: PostgreSQL states that it aims to conform to the latest standard where that does not contradict traditional features or common sense.
Criticism of the language began with its own creators. Chamberlin's remark that the first standard "kept all the warts" refers to inconsistencies carried over unchanged from IBM's early implementation, and Gray recalled Chamberlin returning from a meeting at IBM's Santa Teresa laboratory to report that it had spent the entire day deciding how to spell NULL.