Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the INSERT statement do in SQL when adding a new row to a table with an auto-generated key?
easy
A. It updates the existing row with a new key.
B. It deletes an existing row based on the key.
C. It adds a new row and automatically creates a unique ID for that row.
D. It only creates a key without adding any row.

Solution

  1. Step 1: Understand the purpose of INSERT

    The INSERT statement adds new rows to a table in a database.
  2. Step 2: Recognize auto-generated keys

    When a table has an auto-generated key, the database automatically creates a unique ID for each new row inserted.
  3. Final Answer:

    It adds a new row and automatically creates a unique ID for that row. -> Option C
  4. Quick Check:

    INSERT adds row + auto key = It adds a new row and automatically creates a unique ID for that row. [OK]
Hint: INSERT adds rows; auto keys create unique IDs [OK]
Common Mistakes:
  • Thinking INSERT deletes or updates rows
  • Assuming keys must be manually provided
  • Confusing INSERT with SELECT or DELETE
2. Which of the following is the correct syntax to insert a new row into a table users with columns name and email, letting the database auto-generate the id?
easy
A. INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com');
B. INSERT users (name, email) VALUES ('Alice', 'alice@example.com');
C. INSERT INTO users VALUES ('Alice', 'alice@example.com');
D. INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');

Solution

  1. Step 1: Check correct INSERT syntax

    The correct syntax specifies the table name, columns, and values. Omitting the auto-generated key column lets the database handle it.
  2. Step 2: Analyze each option

    INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'); correctly inserts only name and email, letting the database auto-generate the id. INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com'); provides an explicit id=1, overriding auto-generation. INSERT INTO users VALUES ('Alice', 'alice@example.com'); misses column names and provides wrong number of values (2 vs 3 columns), causing error. INSERT users (name, email) VALUES ('Alice', 'alice@example.com'); has wrong syntax (missing INTO).
  3. Final Answer:

    INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'); -> Option D
  4. Quick Check:

    Omit auto key column to auto-generate [OK]
Hint: Omit auto key column in INSERT to auto-generate [OK]
Common Mistakes:
  • Including auto-generated key column with a manual value
  • Missing INTO keyword in INSERT
  • Not specifying columns when table has auto keys
3. Given the table orders with columns order_id (auto-generated), product, and quantity, what will this query return?
INSERT INTO orders (product, quantity) VALUES ('Book', 3) RETURNING order_id;
medium
A. The new order_id generated for the inserted row.
B. The number of rows inserted (1).
C. An error because RETURNING is not valid here.
D. The product and quantity values inserted.

Solution

  1. Step 1: Understand RETURNING clause

    RETURNING lets you get values from the inserted row immediately, such as auto-generated keys.
  2. Step 2: Analyze the query

    The query inserts a new order with product 'Book' and quantity 3, then returns the auto-generated order_id.
  3. Final Answer:

    The new order_id generated for the inserted row. -> Option A
  4. Quick Check:

    INSERT ... RETURNING returns new keys [OK]
Hint: RETURNING fetches new auto-generated keys after insert [OK]
Common Mistakes:
  • Thinking RETURNING returns row count
  • Assuming RETURNING is invalid syntax
  • Expecting all inserted columns returned by default
4. What is wrong with this SQL statement if id is an auto-generated key?
INSERT INTO customers (id, name) VALUES (NULL, 'John');
medium
A. Using NULL for id may cause an error or fail to auto-generate the key.
B. The syntax is correct and will auto-generate id.
C. You must provide a valid id value manually.
D. The table name is missing.

Solution

  1. Step 1: Understand auto-generated key behavior

    Auto-generated keys (like SERIAL in Postgres or AUTO_INCREMENT) generate a value when the column is omitted, explicitly set to NULL, or DEFAULT. Explicit non-NULL values override it.
  2. Step 2: Analyze the statement

    INSERT INTO customers (id, name) VALUES (NULL, 'John'); explicitly sets id to NULL, which invokes the default generator, so the database auto-generates the next unique id. Syntax is correct.
  3. Final Answer:

    The syntax is correct and will auto-generate id. -> Option B
  4. Quick Check:

    NULL in auto-key column triggers generation [OK]
Hint: NULL in auto-key triggers generation; omit also works [OK]
Common Mistakes:
  • Thinking explicit NULL causes an error or inserts NULL
  • Believing you must omit the column and can't use NULL
  • Confusing NULL with providing a manual non-NULL id
5. You want to insert multiple rows into a products table with an auto-generated product_id and get all new IDs immediately. Which SQL approach is best?
hard
A. Use a single INSERT with multiple rows and RETURNING to get all new IDs at once.
B. Use multiple INSERT statements each with RETURNING to get IDs one by one.
C. Insert rows without RETURNING and then query the table for new IDs by timestamp.
D. Manually generate IDs in your application and insert them with the rows.

Solution

  1. Step 1: Understand batch insert with RETURNING

    Modern SQL supports inserting multiple rows in one statement and returning all generated keys at once.
  2. Step 2: Compare options

    Use a single INSERT with multiple rows and RETURNING to get all new IDs at once. is efficient and returns all new IDs immediately. Use multiple INSERT statements each with RETURNING to get IDs one by one. is slower with multiple statements. Insert rows without RETURNING and then query the table for new IDs by timestamp. is unreliable and complex. Manually generate IDs in your application and insert them with the rows. defeats auto-generation purpose.
  3. Final Answer:

    Use a single INSERT with multiple rows and RETURNING to get all new IDs at once. -> Option A
  4. Quick Check:

    Batch INSERT + RETURNING = all IDs fast [OK]
Hint: Batch insert with RETURNING gets all keys at once [OK]
Common Mistakes:
  • Running many single inserts instead of batch
  • Querying after insert instead of RETURNING
  • Manually creating keys instead of auto-generation
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