Challenge - 5 Problems
Auto-Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output after inserting a row with auto-generated key?
Consider a table Users with columns
id (auto-increment primary key) and name. After running the following SQL statements, what will be the value of id for the newly inserted row?INSERT INTO Users (name) VALUES ('Alice');
SELECT id FROM Users WHERE name = 'Alice';SQL
CREATE TABLE Users (id SERIAL PRIMARY KEY, name VARCHAR(100)); INSERT INTO Users (name) VALUES ('Alice'); SELECT id FROM Users WHERE name = 'Alice';
Attempts:
2 left
💡 Hint
Auto-increment keys start from 1 by default and increase by 1 for each new row.
✗ Incorrect
When you insert a row without specifying the auto-increment column, the database automatically assigns the next available number starting at 1. So the first inserted row gets id = 1.
🧠 Conceptual
intermediate1:30remaining
Which SQL clause retrieves the auto-generated key after an INSERT?
You insert a new row into a table with an auto-generated primary key. Which SQL clause or statement lets you get the value of that new key immediately after insertion?
Attempts:
2 left
💡 Hint
Think about how to get data back from an INSERT in one step.
✗ Incorrect
The RETURNING clause in SQL lets you get the values of columns, including auto-generated keys, right after inserting a row.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this INSERT with auto-generated key retrieval
Which option contains a syntax error when trying to insert a row and get the auto-generated key in PostgreSQL?
SQL
INSERT INTO Orders (product) VALUES ('Book') RETURNING id;
Attempts:
2 left
💡 Hint
Check the order of clauses in the INSERT statement.
✗ Incorrect
The RETURNING clause must come after the VALUES clause. Option A places RETURNING before VALUES, which is invalid syntax.
❓ optimization
advanced2:30remaining
Best way to insert multiple rows and get all auto-generated keys efficiently
You want to insert multiple rows into a table with an auto-increment key and get all their generated keys in one query. Which option is the best approach?
Attempts:
2 left
💡 Hint
Think about minimizing round trips to the database.
✗ Incorrect
A single INSERT with multiple rows and RETURNING returns all generated keys at once, which is efficient and reduces database calls.
🔧 Debug
expert3:00remaining
Why does this INSERT fail to return the auto-generated key?
Given the table
Why does this happen if you want to get the new id?
Products(id SERIAL PRIMARY KEY, name TEXT), the following query runs but returns no rows:INSERT INTO Products (name) VALUES ('Pen');Why does this happen if you want to get the new id?
Attempts:
2 left
💡 Hint
Think about how to get data back from an INSERT statement.
✗ Incorrect
Without RETURNING, the INSERT does not output any rows. To get the new id, you must add RETURNING id.