Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

returning-clause

RETURNING clause

Last reviewed Jun 1, 2026 Content v20260601
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~1 min
Level
beginner

This lesson

This lesson teaches RETURNING clause: the SQL patterns, schema habits, and query reasoning you need before advancing in PostgreSQL.

Teams query RETURNING clause on every PostgreSQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply RETURNING clause in contexts like: Modern startups, geospatial apps, and analytics-friendly OLTP systems.

Copy Postgres SQL into psql, local PostgreSQL, or DB Fiddle (PostgreSQL dialect)—use \d and EXPLAIN ANALYZE where lessons show them. The in-browser lab ships later; psql is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Postgres RETURNING projects rows affected by INSERT, UPDATE, or DELETE—eliminating extra round trips to fetch generated keys or audit changes.

INSERT … RETURNING

INSERT INTO customers (email)
VALUES ('ada@example.com')
RETURNING id, email, created_at;

Practice: Run in psql or DB Fiddle (PostgreSQL). Use your practice database from the workflow lesson.

UPDATE and DELETE

UPDATE customers
SET email = 'ada+new@example.com'
WHERE id = 1
RETURNING id, email;

DELETE FROM customers
WHERE id = 999
RETURNING id;

API and ORM pattern

REST handlers often return the created row body from RETURNING instead of a second SELECT. Django querysets can use .values() after create or raw SQL with RETURNING for bulk inserts.

Important interview questions and answers

  1. Q: RETURNING with multiple rows?
    A: Works for any DML affecting rows—returns all matching rows.
  2. Q: MySQL equivalent?
    A: MySQL lacks RETURNING in older versions; Postgres has had it for years—portable apps may still SELECT separately.

Self-check

  1. How do you get a new SERIAL id after INSERT?
  2. Can UPDATE use RETURNING?

Tip: Use RETURNING in APIs to avoid a second round trip for new primary keys.

Interview prep

INSERT RETURNING?

Projects inserted row including generated keys.

DELETE RETURNING?

Returns deleted rows for auditing or APIs.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • RETURNING * risk?
  • Upsert RETURNING?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump