Learning path

Full curriculum

Full curriculum

Unit content

SQL queries

SQL is a declarative language for working with relational databases. A query describes the result wanted rather than prescribing the physical algorithm the database must use to obtain it.

Selecting rows and columns

A simple query is

SELECT name, active
FROM users
WHERE active = true;

FROM identifies the source relation, WHERE filters rows and SELECT chooses the output expressions or columns.

Ordering and limiting results

Relational rows have no guaranteed order unless the query requests one:

SELECT name
FROM users
ORDER BY name
LIMIT 10;

Changing data

SQL also contains statements for modifying relations. Typical forms include

INSERT INTO users (name) VALUES ('Ada');
UPDATE users SET active = false WHERE id = 1;
DELETE FROM users WHERE id = 1;

The WHERE condition is especially important because an update or deletion without an intended filter can affect many rows.

NULL

Missing or unknown values represented by NULL require special predicates such as

WHERE email IS NULL

rather than ordinary equality.

SQL expresses operations in terms of relations and values. The database system chooses execution plans, indexes and physical access strategies separately from the logical query.