Stop 09 · Modifying DataTopics · 8/9

RETURNING and Generated IDs

Part of the SQL Roadmap

In short

You insert an order and the app immediately needs its new order ID to save the order items. RETURNING hands that ID back in the same statement, with no second query and no guessing.

After an INSERT you often need the new row's generated ID. PostgreSQL returns it with RETURNING, SQL Server uses OUTPUT, and MySQL uses LAST_INSERT_ID() after the insert.

The jaadu sits behind a wooden shoe rack outside a temple, taking a pair of sandals with one hand and handing back a round brass token with the other.

Think of it like this

The shoe stand token at a temple

At the shoe stand outside a temple in Madurai, you hand over your chappals and the attendant gives you a numbered token on the spot. You don't walk off and later ask 'which number did I get?'. RETURNING is that token: the database gives you the new ID the moment it stores the row.

Watch it happen

INSERT ... RETURNING order_id

App
orders
order_items

INSERT order

1/4The app inserts a new order for Farhan in Kochi worth ₹1,899. It doesn't send an ID.

The big ideas

What you need to know

Works on all writes

In PostgreSQL, INSERT, UPDATE and DELETE can all end with RETURNING to show the affected rows.

Never use MAX(id)

Another user may insert between your insert and your SELECT MAX. RETURNING gives you your own row's ID.

Chain with a CTE

WITH new_order AS (INSERT ... RETURNING order_id) lets the next INSERT use that ID in the same statement.

Other databases

SQL Server uses OUTPUT inserted.order_id. MySQL uses SELECT LAST_INSERT_ID() right after the insert.

See it in code

Walk through the code

sql
1INSERT INTO orders (customer_id, amount, city)2VALUES (42, 1899.00, 'Kochi')3RETURNING order_id, created_at;4 5WITH new_order AS (6  INSERT INTO orders (customer_id, amount, city)7  VALUES (42, 1899.00, 'Kochi')8  RETURNING order_id9)10INSERT INTO order_items (order_id, product_id, quantity)11SELECT order_id, 7, 212FROM new_order;

1/3Insert the order and get back its generated order_id and the default created_at.

Get the new ID back, then use it straight away in one statement. RETURNING is PostgreSQL (also SQLite and MariaDB). In SQL Server, put OUTPUT inserted.order_id before VALUES. In MySQL, call LAST_INSERT_ID() after the insert.

Save yourself the pain

Common mistakes

Oops

Running SELECT MAX(order_id) FROM orders right after an insert.

Do this instead

With many users, MAX may belong to someone else's order. Use RETURNING, OUTPUT or LAST_INSERT_ID().

Oops

Running a separate SELECT to see what an UPDATE changed.

Do this instead

Add RETURNING to the UPDATE itself to see the exact rows and new values it changed.

Your mission

Order and items in one go

~20 min

Quick check

Why is RETURNING safer than SELECT MAX(order_id) after an insert?

Go deeper

  1. 1Insert an order and get its new ID back in the same statement in PostgreSQL.
  2. 2Use the returned ID to insert the order's items.
  3. 3Try RETURNING with UPDATE and DELETE to see which rows changed.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge