SQL reference (MySQL and MariaDB dialects)

RETURNING clause

INSERT, REPLACE and DELETE ... RETURNING return the rows they wrote or removed as a result set (MariaDB form).

RETURNING makes a write statement answer with a result set of the rows it wrote or removed (MariaDB's feature, D81). The parser accepts it after INSERT, REPLACE and DELETE without testing the dialect; the behaviour is verified against MariaDB 11.4. MySQL has no RETURNING.

Syntax#

SQL
INSERT ... [RETURNING select_item [, select_item] ...]
REPLACE ... [RETURNING select_item [, select_item] ...]
DELETE FROM tbl_name [WHERE ...] [ORDER BY ...] [LIMIT n] [RETURNING select_item [, select_item] ...]

select_item:
    * | expr [[AS] alias]

Arguments#

NameMeaningDefault
select_itemColumns or expressions of the written (or removed) row; * is all visible columns.none

Remarks#

  • The items are checked before anything is written: an unknown column fails the statement with nothing changed.
  • The rows are those the statement wrote or removed. For INSERT ... SELECT and multi-row VALUES there is one result row per written row.
  • INSERT IGNORE ... RETURNING gives back the auto-increment value of a row that IGNORE left out, as MariaDB does.
  • REPLACE ... RETURNING returns the new rows.
  • UPDATE ... RETURNING is not in the grammar (MariaDB 11.4 does not have it either).
  • The privileges are those of the write statement.

Examples#

Example:

SQL
INSERT INTO t (name, qty) VALUES ('a', 1), ('b', 2), ('c', 3) RETURNING id, name;
INSERT INTO t (name) VALUES ('d') RETURNING *;
INSERT INTO t (name, qty) VALUES ('e', 7) RETURNING id AS new_id, qty * 2 AS doubled, CONCAT(name, '!') AS shouted;
DELETE FROM t WHERE id IN (2, 3) RETURNING id, name;
DELETE FROM t ORDER BY id LIMIT 2 RETURNING id;

See also#