Skip to content
Part IA Michaelmas Term

What is a Query Language?

Definition

A relational database query language takes a collection of relation instances R1,R2,,RkR_1, R_2, \ldots, R_k as input and returns a single relation instance as output:

Q(R1,R2,,Rk)Q(R_1, R_2, \ldots, R_k)

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

FormNameMeaning
Q::=RQ ::= RBase relationThe relation RR itself
Q::=σp(Q)Q ::= \sigma_p(Q)SelectionRows satisfying predicate pp
Q::=πX(Q)Q ::= \pi_X(Q)ProjectionColumns in set XX, duplicates removed
Q::=Q×QQ ::= Q \times QProductConcatenation of every pair of tuples
Q::=QQQ ::= Q - QDifferenceTuples in first but not second
Q::=QQQ ::= Q \cup QUnionTuples in either (or both)
Q::=QQQ ::= Q \cap QIntersectionTuples in both
Q::=ρM(Q)Q ::= \rho_M(Q)RenamingChange column names per map MM

Where:

  • pp is a Boolean predicate over attribute values (e.g., A>12A > 12, B=’CS’B = \text{'CS'}).
  • XX is a set of attribute names to keep.
  • MM is a renaming map, e.g., {BB}\{B \mapsto B'\}.

Well-formedness conditions

A query QQ must be well-formed: all column names of the result must be distinct. This imposes constraints on certain operators:

  • In Q1×Q2Q_1 \times Q_2, the two sub-queries cannot share any column names (they must be disjoint).
  • In Q1Q2Q_1 \cup Q_2, Q1Q2Q_1 \cap Q_2, and Q1Q2Q_1 - Q_2, the two sub-queries must share all column names (they must be union-compatible).

If needed, ρ\rho 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:

VersionYearKey features
SQL-861986First standard
SQL-891989Minor revision
SQL-921992Major expansion (outer joins, CHECK constraints)
SQL:19991999Recursive queries, triggers, OO features
SQL:20032003Window functions, XML support
SQL:20082008MERGE, TRUNCATE
SQL:20112011Temporal databases
SQL:20162016JSON support
SQL:20232023Property 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?

  1. RA’s simple syntax and semantics make it easier to reason about complex queries. Understanding what a query means in RA helps debug SQL.
  2. The RA lends itself to Tripos questions. It provides a compact, unambiguous notation for defining queries — ideal for exam settings.
  3. 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.