What is a Query Language?
Definition
A relational database query language takes a collection of relation instances as input and returns a single relation instance as output:
To meet Codd’s goals, the query language should be high-level: the user specifies what data they want, not how to retrieve it. The language is independent of physical data representation — indexes, file structures, and access paths are managed internally by the DBMS.
Relational Algebra (RA)
The Relational Algebra is the formal basis for relational query languages. It provides a small set of operations that take relations as input and produce relations as output. Because each operation returns a relation, operations can be composed to form complex queries.
Abstract syntax
| Form | Name | Meaning |
|---|---|---|
| Base relation | The relation itself | |
| Selection | Rows satisfying predicate | |
| Projection | Columns in set , duplicates removed | |
| Product | Concatenation of every pair of tuples | |
| Difference | Tuples in first but not second | |
| Union | Tuples in either (or both) | |
| Intersection | Tuples in both | |
| Renaming | Change column names per map |
Where:
- is a Boolean predicate over attribute values (e.g., , ).
- is a set of attribute names to keep.
- is a renaming map, e.g., .
Well-formedness conditions
A query must be well-formed: all column names of the result must be distinct. This imposes constraints on certain operators:
- In , the two sub-queries cannot share any column names (they must be disjoint).
- In , , and , the two sub-queries must share all column names (they must be union-compatible).
If needed, is used to rename columns to satisfy these conditions.
SQL
SQL (Structured Query Language) is the dominant relational query language. It originated at IBM in the early 1970s and has been standardised across multiple revisions:
| Version | Year | Key features |
|---|---|---|
| SQL-86 | 1986 | First standard |
| SQL-89 | 1989 | Minor revision |
| SQL-92 | 1992 | Major expansion (outer joins, CHECK constraints) |
| SQL:1999 | 1999 | Recursive queries, triggers, OO features |
| SQL:2003 | 2003 | Window functions, XML support |
| SQL:2008 | 2008 | MERGE, TRUNCATE |
| SQL:2011 | 2011 | Temporal databases |
| SQL:2016 | 2016 | JSON support |
| SQL:2023 | 2023 | Property graph queries, ANY_VALUE |
SQL contains several sub-languages:
- Query Language (QL):
SELECT,FROM,WHERE,GROUP BY, etc. - Data Definition Language (DDL):
CREATE TABLE,ALTER TABLE,DROP TABLE. - Data Manipulation Language (DML):
INSERT,UPDATE,DELETE. - Data Control Language (DCL):
GRANT,REVOKE. - System Administration Language: backup, restore, user management.
Why study RA if we have SQL?
- RA’s simple syntax and semantics make it easier to reason about complex queries. Understanding what a query means in RA helps debug SQL.
- The RA lends itself to Tripos questions. It provides a compact, unambiguous notation for defining queries — ideal for exam settings.
- SQL is vast and continually evolving. RA captures its core relational logic in a handful of operators.
Summary
- A query language maps a collection of relation instances to a single relation instance.
- RA provides the formal mathematical basis: six core operations plus renaming.
- SQL is the practical language, standardised from SQL-86 through SQL:2023.
- RA is studied for clarity and exam support; SQL is used in practice.