Skip to main content

RETURNING Clause

RETURNING lets write operations return row data right away.

This removes extra round-trips where you would otherwise run a second SELECT.

Core Concepts

flowchart LR
A[INSERT/UPDATE/DELETE] --> B[Apply data change]
B --> C[RETURNING columns]
C --> D[Immediate result rows]
OperationExample
Insert + returningINSERT ... RETURNING id;
Update + returningUPDATE ... RETURNING *;
Delete + returningDELETE ... RETURNING name;
CREATE TABLE tickets (
ticket_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
status TEXT DEFAULT 'open'
);

INSERT INTO tickets (title)
VALUES ('Fix login bug')
RETURNING ticket_id, title, status;
ticket_idtitlestatus
1Fix login bugopen

Code Examples

-- Insert and return generated key in one step.
INSERT INTO tickets (title)
VALUES ('Write docs')
RETURNING ticket_id AS new_id;
new_id
2

SQLite-Specific Nuances

SQLite Nuance

RETURNING is available in SQLite 3.35.0 and newer.

If you are on an older version, you must query again after write operations.

Common Pitfalls / Best Practices

Pitfall

Using RETURNING in environments with older SQLite versions and assuming it always works.

Best Practice

Check sqlite3 --version and keep write statements + returned columns minimal and intentional.

Quick Challenge

Insert a row into tickets with title Review PR and return ticket_id plus status.

View Solution
INSERT INTO tickets (title)
VALUES ('Review PR')
RETURNING ticket_id, status;
ticket_idstatus
e.g. 3open