Challenge - 5 Problems
PostgreSQL INSERT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
The RETURNING clause returns the specified columns of the inserted row.
✗ Incorrect
The RETURNING clause outputs the columns requested from the newly inserted row. Here, it returns the generated id and the name 'Alice'.
📝 Syntax
intermediate2: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).Attempts:
2 left
💡 Hint
The keyword is RETURNING without parentheses.
✗ Incorrect
PostgreSQL uses RETURNING without parentheses to specify columns to return after an INSERT.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Think about efficiency and data consistency.
✗ Incorrect
Using RETURNING returns the inserted row's data immediately, avoiding extra queries and potential conflicts.
🔧 Debug
advanced2: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;Attempts:
2 left
💡 Hint
Check the NOT NULL constraint on amount.
✗ Incorrect
Inserting NULL into a NOT NULL column causes a constraint violation error.
🧠 Conceptual
expert3:00remaining
How does RETURNING behave with multi-row INSERT?
Consider this query inserting multiple rows:
What will the RETURNING clause output?
INSERT INTO employees(name, salary) VALUES ('John', 5000), ('Jane', 6000) RETURNING id, name;What will the RETURNING clause output?
Attempts:
2 left
💡 Hint
RETURNING returns data for all inserted rows.
✗ Incorrect
RETURNING outputs one row per inserted row, showing requested columns for each.