×

sql · level 3 · unit 6 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

hands back the rows a change touched.

After an INSERT, you often need the new row's id, which the database chose. After an UPDATE, you want the new values. Asking again with a second SELECT costs another trip, and it can pick up the wrong row.

Add RETURNING and a list of columns to the end of an INSERT, UPDATE or DELETE. The statement then hands back the rows it changed, like a receipt. RETURNING * gives every column.

One warning: when several rows change, the order of the returned rows is not promised.

INSERT INTO books (title, price, stock)
VALUES ('Ocean Cats', 7, 3)
RETURNING id, title;

RETURNING shows the row after the change, including values the database filled in, like the id.