Challenge - 5 Problems
AUTO_INCREMENT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this AUTO_INCREMENT insert sequence?
Consider a table
users with an id column set as AUTO_INCREMENT starting at 1. After inserting three rows, the id values are 1, 2, and 3. If we delete the row with id = 2 and then insert a new row, what will be the id of the new row?SQL
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); INSERT INTO users (name) VALUES ('Alice'), ('Bob'), ('Charlie'); DELETE FROM users WHERE id = 2; INSERT INTO users (name) VALUES ('David'); SELECT id, name FROM users ORDER BY id;
Attempts:
2 left
💡 Hint
AUTO_INCREMENT does not reuse deleted IDs by default.
✗ Incorrect
When a row with an AUTO_INCREMENT ID is deleted, the next inserted row gets a new ID incremented from the highest existing ID. It does not reuse the deleted ID.
🧠 Conceptual
intermediate1:30remaining
How does AUTO_INCREMENT behave after a server restart?
If a MySQL server is restarted, what happens to the AUTO_INCREMENT counter for a table that already has rows?
Attempts:
2 left
💡 Hint
Think about how the server remembers the last used ID.
✗ Incorrect
On restart, MySQL reads the highest existing AUTO_INCREMENT value from the table and continues from there to avoid duplicate IDs.
📝 Syntax
advanced2:00remaining
Which statement correctly creates a table with AUTO_INCREMENT starting at 1000?
You want to create a table
orders with an order_id column that auto-increments starting from 1000. Which SQL statement is correct?Attempts:
2 left
💡 Hint
The AUTO_INCREMENT starting value is set outside the column definition.
✗ Incorrect
In MySQL, the starting value for AUTO_INCREMENT is set as a table option after the column definitions using AUTO_INCREMENT=number.
🔧 Debug
advanced2:00remaining
Why does this insert fail with duplicate key error?
Given the table
products with product_id as AUTO_INCREMENT primary key, the following insert causes a duplicate key error. Why?
INSERT INTO products (product_id, name) VALUES (1, 'Pen');SQL
CREATE TABLE products (product_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); INSERT INTO products (name) VALUES ('Pencil'); INSERT INTO products (product_id, name) VALUES (1, 'Pen');
Attempts:
2 left
💡 Hint
Check the existing IDs before the insert.
✗ Incorrect
The first insert auto-assigned product_id = 1. Trying to insert another row with product_id = 1 causes a duplicate primary key error.
❓ optimization
expert3:00remaining
How to optimize bulk inserts with AUTO_INCREMENT to avoid gaps?
You need to insert 10,000 rows into a table with an AUTO_INCREMENT primary key. You want to minimize gaps in the IDs caused by transaction rollbacks or deletes. Which approach helps achieve this?
Attempts:
2 left
💡 Hint
Think about how transactions affect AUTO_INCREMENT values.
✗ Incorrect
Using a single transaction for bulk inserts avoids gaps caused by rollbacks because IDs are only assigned once the transaction commits.