Bird
Raised Fist0
PostgreSQLquery~20 mins

INSERT with RETURNING clause in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PostgreSQL INSERT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What does this INSERT with RETURNING output?
Given the table users(id SERIAL PRIMARY KEY, name TEXT, age INT), what is the output of this query?

INSERT INTO users (name, age) VALUES ('Alice', 30) RETURNING id, name;
PostgreSQL
INSERT INTO users (name, age) VALUES ('Alice', 30) RETURNING id, name;
A[{"name": "Alice", "age": 30}]
B[{"id": 1, "name": "Alice"}]
CNo output, just inserts the row
DSyntax error
Attempts:
2 left
💡 Hint
The RETURNING clause returns the specified columns of the inserted row.
📝 Syntax
intermediate
2:00remaining
Which INSERT with RETURNING syntax is correct?
Choose the correct syntax to insert a row and return the generated id and age from table people(id SERIAL, age INT).
AINSERT INTO people (age) VALUES (25) RETURNING id, age;
BINSERT INTO people (age) VALUES (25) RETURN id, age;
CINSERT INTO people (age) VALUES (25) RETURNING (id, age);
DINSERT INTO people (age) VALUES (25) OUTPUT id, age;
Attempts:
2 left
💡 Hint
The keyword is RETURNING without parentheses.
❓ optimization
advanced
2:00remaining
Why use INSERT with RETURNING instead of separate SELECT?
You want to insert a new product and get its generated id immediately. Which is the main advantage of using INSERT ... RETURNING id over inserting then running a separate SELECT to get the id?
AIt returns all columns by default without specifying.
BIt automatically commits the transaction.
CIt locks the entire table to prevent other inserts.
DIt reduces the number of queries and avoids race conditions.
Attempts:
2 left
💡 Hint
Think about efficiency and data consistency.
🔧 Debug
advanced
2:00remaining
What error does this INSERT with RETURNING cause?
Given table orders(order_id SERIAL PRIMARY KEY, amount INT NOT NULL), what error occurs with this query?

INSERT INTO orders (amount) VALUES (NULL) RETURNING order_id;
ASyntax error near RETURNING
BERROR: duplicate key value violates unique constraint
CERROR: null value in column "amount" violates not-null constraint
DNo error, returns order_id
Attempts:
2 left
💡 Hint
Check the NOT NULL constraint on amount.
🧠 Conceptual
expert
3:00remaining
How does RETURNING behave with multi-row INSERT?
Consider this query inserting multiple rows:

INSERT INTO employees(name, salary) VALUES ('John', 5000), ('Jane', 6000) RETURNING id, name;

What will the RETURNING clause output?
AA result set with two rows, each containing the id and name of the inserted employees.
BAn error because RETURNING cannot be used with multiple rows.
CNo output because RETURNING works only for single-row inserts.
DOnly the id and name of the first inserted row.
Attempts:
2 left
💡 Hint
RETURNING returns data for all inserted rows.

Practice

(1/5)
1. What does the RETURNING clause do in a PostgreSQL INSERT statement?
easy
A. It creates a backup of the inserted data.
B. It deletes rows after insertion.
C. It updates existing rows instead of inserting.
D. It returns specified columns of the newly inserted rows immediately.

Solution

  1. Step 1: Understand the purpose of RETURNING

    The RETURNING clause in PostgreSQL is used to get back specified columns from the rows that were just inserted.
  2. Step 2: Compare with other options

    The other options describe actions unrelated to RETURNING. RETURNING does not delete, update, or backup data.
  3. Final Answer:

    It returns specified columns of the newly inserted rows immediately. -> Option D
  4. Quick Check:

    RETURNING returns inserted data [OK]
Hint: RETURNING fetches inserted row data instantly [OK]
Common Mistakes:
  • Thinking RETURNING deletes or updates data
  • Confusing RETURNING with SELECT
  • Assuming RETURNING creates backups
2. Which of the following is the correct syntax to insert a row and return the generated id in PostgreSQL?
easy
A. INSERT INTO users VALUES ('Alice') RETURNING id;
B. INSERT INTO users (name) RETURN id VALUES ('Alice');
C. INSERT INTO users (name) VALUES ('Alice') RETURNING id;
D. INSERT INTO users (name) VALUES ('Alice') RETURN id;

Solution

  1. Step 1: Check correct RETURNING syntax

    The correct syntax places RETURNING after VALUES and specifies the column to return, like RETURNING id.
  2. Step 2: Validate each option

    INSERT INTO users (name) VALUES ('Alice') RETURNING id; matches the correct syntax. Placing RETURNING before VALUES or using RETURN instead of RETURNING is incorrect. INSERT INTO users VALUES ('Alice') RETURNING id; misses column list before VALUES.
  3. Final Answer:

    INSERT INTO users (name) VALUES ('Alice') RETURNING id; -> Option C
  4. Quick Check:

    RETURNING follows VALUES with column list [OK]
Hint: RETURNING goes right after VALUES clause [OK]
Common Mistakes:
  • Placing RETURNING before VALUES
  • Omitting column list in INSERT
  • Using RETURN instead of RETURNING
3. Given the table products(id SERIAL PRIMARY KEY, name TEXT, price NUMERIC), what will this query return?
INSERT INTO products (name, price) VALUES ('Pen', 1.5) RETURNING id, price;
medium
A. The new product's id and price, e.g., (5, 1.5)
B. Only the id of the new product
C. An error because RETURNING can't return multiple columns
D. Nothing, INSERT does not return rows

Solution

  1. Step 1: Understand RETURNING with multiple columns

    RETURNING can return one or more columns from the inserted row(s). Here, id and price are requested.
  2. Step 2: Predict output

    The query inserts a new product and returns its auto-generated id and the price 1.5 as a row, e.g., (5, 1.5).
  3. Final Answer:

    The new product's id and price, e.g., (5, 1.5) -> Option A
  4. Quick Check:

    RETURNING multiple columns returns all requested [OK]
Hint: RETURNING can list multiple columns separated by commas [OK]
Common Mistakes:
  • Assuming RETURNING returns only one column
  • Expecting no output from INSERT
  • Thinking RETURNING causes syntax error with multiple columns
4. What is wrong with this query?
INSERT INTO orders (product_id, quantity) VALUES (10, 3) RETURNING;
medium
A. VALUES clause is incorrectly formatted.
B. RETURNING clause is missing column names to return.
C. INSERT statement cannot have RETURNING clause.
D. product_id and quantity columns do not exist.

Solution

  1. Step 1: Check RETURNING syntax

    RETURNING must specify at least one column or * to return. Here, it is empty, which is invalid syntax.
  2. Step 2: Verify other parts

    VALUES clause and columns look correct. INSERT supports RETURNING. Assuming columns exist, no error there.
  3. Final Answer:

    RETURNING clause is missing column names to return. -> Option B
  4. Quick Check:

    RETURNING needs columns or * [OK]
Hint: RETURNING must specify columns or * [OK]
Common Mistakes:
  • Leaving RETURNING empty
  • Confusing RETURNING with WHERE
  • Assuming RETURNING is optional but empty
5. You want to insert multiple rows into employees(name, department) and get back their generated id values. Which query correctly does this and returns all new ids?
hard
A. INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURNING id;
B. INSERT INTO employees (name, department) RETURNING id VALUES ('John', 'HR'), ('Jane', 'IT');
C. INSERT INTO employees (name, department) VALUES ('John', 'HR'); RETURNING id; INSERT INTO employees (name, department) VALUES ('Jane', 'IT'); RETURNING id;
D. INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURN id;

Solution

  1. Step 1: Understand multi-row INSERT with RETURNING

    PostgreSQL allows inserting multiple rows in one statement and RETURNING returns all inserted rows' specified columns.
  2. Step 2: Analyze each option

    INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURNING id; correctly inserts two rows and returns their ids. INSERT INTO employees (name, department) RETURNING id VALUES ('John', 'HR'), ('Jane', 'IT'); misplaces RETURNING before VALUES. INSERT INTO employees (name, department) VALUES ('John', 'HR'); RETURNING id; INSERT INTO employees (name, department) VALUES ('Jane', 'IT'); RETURNING id; splits inserts and RETURNING incorrectly. INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURN id; uses invalid RETURN keyword.
  3. Final Answer:

    INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURNING id; -> Option A
  4. Quick Check:

    Multi-row INSERT with RETURNING returns all rows [OK]
Hint: Use RETURNING after multi-row VALUES to get all ids [OK]
Common Mistakes:
  • Placing RETURNING before VALUES
  • Splitting inserts instead of multi-row insert
  • Using RETURN instead of RETURNING
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard