IT lexicon › Database › RETURNING clause

RETURNING clause

Database På svenska → Updated: 2026-05-28

PostgreSQL extension: INSERT/UPDATE/DELETE returns the modified rows. INSERT INTO users (name) VALUES ('Alice') RETURNING id, created_at — avoid a separate SELECT after INSERT for the auto-generated ID. Standardized in SQL:2023 but a Postgres feature since 8.2 (2006).

Use case: get auto-generated PK after INSERT (alternative: LAST_INSERT_ID() in MySQL). UPDATE ... RETURNING: get pre-existing values for audit. DELETE ... RETURNING: cleanup + report what was deleted. Compose with CTEs: WITH inserted AS (INSERT ... RETURNING *) SELECT * FROM inserted JOIN .... Performance: single roundtrip vs INSERT + SELECT separate. MySQL: added RETURNING syntax first in MariaDB 10.5 (2020), MySQL 8.x still lacks it (2024). ORMs (SQLAlchemy, Django) use RETURNING under the hood when available.

← Back to the lexicon