0
0
PostgreSQLquery~20 mins

RETURNING clause mental model in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RETURNING Clause 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?
Consider the table users(id SERIAL PRIMARY KEY, name TEXT, age INT). What will be 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;
AA single row with columns <code>id</code> and <code>name</code> showing the new user's id and name.
BA list of all users with their <code>id</code> and <code>name</code>.
CAn error because RETURNING cannot be used with INSERT.
DNo output; the query only inserts data silently.
Attempts:
2 left
💡 Hint
RETURNING lets you get back data from the rows you just inserted.
query_result
intermediate
2:00remaining
What does this UPDATE with RETURNING output?
Given the table products(id INT PRIMARY KEY, price NUMERIC), what will this query output?
UPDATE products SET price = price * 1.1 WHERE id = 5 RETURNING id, price;
PostgreSQL
UPDATE products SET price = price * 1.1 WHERE id = 5 RETURNING id, price;
AAn error because RETURNING is not allowed with UPDATE.
BThe entire products table with updated prices.
CNo output; UPDATE does not return rows.
DThe <code>id</code> and updated <code>price</code> of the product with id 5.
Attempts:
2 left
💡 Hint
RETURNING shows the rows that were changed.
📝 Syntax
advanced
2:00remaining
Which query correctly uses RETURNING with DELETE?
You want to delete a user with id 10 and get back their name. Which query is correct?
ADELETE FROM users WHERE id = 10 RETURNING name;
BDELETE FROM users RETURNING name WHERE id = 10;
CDELETE RETURNING name FROM users WHERE id = 10;
DDELETE FROM users WHERE id = 10; RETURNING name;
Attempts:
2 left
💡 Hint
RETURNING comes right after the WHERE clause in DELETE.
optimization
advanced
2:00remaining
Why use RETURNING instead of a separate SELECT after INSERT?
You want to insert a row and get its generated id. Why is using RETURNING better than inserting then selecting?
ARETURNING locks the table, preventing other queries.
BRETURNING duplicates the inserted row in the table.
CRETURNING returns the data immediately without extra queries, improving performance.
DUsing RETURNING causes the insert to fail if the SELECT fails.
Attempts:
2 left
💡 Hint
Think about how many queries you send to the database.
🧠 Conceptual
expert
2:00remaining
What happens if RETURNING is used with a statement that affects zero rows?
If you run UPDATE users SET age = age + 1 WHERE id = 999 RETURNING id; but no user has id 999, what is the output?
PostgreSQL
UPDATE users SET age = age + 1 WHERE id = 999 RETURNING id;
AThe id 999 is returned anyway.
BAn empty result set with zero rows returned.
CAn error saying no rows were updated.
DThe query hangs waiting for a matching row.
Attempts:
2 left
💡 Hint
Think about what happens when no rows match the WHERE condition.