0
0
SQLquery~20 mins

INSERT and auto-generated keys in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Auto-Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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';
AThe id will be 0 because auto-increment starts at zero.
BThe id will be NULL because it is not specified in the INSERT.
CThe id will be 1 if this is the first row inserted.
DThe id will be the name 'Alice' because it is used as the key.
Attempts:
2 left
💡 Hint
Auto-increment keys start from 1 by default and increase by 1 for each new row.
🧠 Conceptual
intermediate
1: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?
AGROUP BY clause after INSERT
BORDER BY clause in the INSERT statement
CWHERE clause in the INSERT statement
DRETURNING clause (e.g., INSERT ... RETURNING id)
Attempts:
2 left
💡 Hint
Think about how to get data back from an INSERT in one step.
📝 Syntax
advanced
2: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;
AINSERT INTO Orders (product) RETURNING id VALUES ('Book');
BINSERT INTO Orders (product) VALUES ('Book') RETURNING id;
CINSERT INTO Orders (product) VALUES ('Book'); SELECT id FROM Orders;
DINSERT INTO Orders (product) VALUES ('Book') RETURNING *;
Attempts:
2 left
💡 Hint
Check the order of clauses in the INSERT statement.
optimization
advanced
2: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?
AUse a single INSERT with multiple VALUES and RETURNING id
BRun multiple single-row INSERTs each with RETURNING id
CInsert rows without RETURNING and then query the table for max id
DInsert rows and use a trigger to store keys in another table
Attempts:
2 left
💡 Hint
Think about minimizing round trips to the database.
🔧 Debug
expert
3:00remaining
Why does this INSERT fail to return the auto-generated key?
Given the table 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?
ABecause the SERIAL type does not auto-generate keys automatically
BBecause the INSERT statement lacks a RETURNING clause to output the new id
CBecause the name 'Pen' is already in the table causing a silent failure
DBecause the id column must be included explicitly in the INSERT
Attempts:
2 left
💡 Hint
Think about how to get data back from an INSERT statement.