0
0
MySQLquery~20 mins

AUTO_INCREMENT behavior in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AUTO_INCREMENT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the next AUTO_INCREMENT value after insert?

Consider this MySQL table and inserts:

CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20));
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
INSERT INTO users (id, name) VALUES (5, 'Charlie');
INSERT INTO users (name) VALUES ('David');

What will be the id of 'David'?

MySQL
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20));
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
INSERT INTO users (id, name) VALUES (5, 'Charlie');
INSERT INTO users (name) VALUES ('David');
SELECT * FROM users ORDER BY id;
A6
B3
C7
D5
Attempts:
2 left
💡 Hint

Remember that AUTO_INCREMENT will use the highest existing value plus one.

🧠 Conceptual
intermediate
1:30remaining
What happens when you delete the highest AUTO_INCREMENT row?

Given a table with AUTO_INCREMENT primary key, if you delete the row with the highest id, what will be the next inserted row's id?

AIt throws an error on insert
BIt continues with the next number after the deleted id
CIt resets to 1
DIt reuses the deleted highest id
Attempts:
2 left
💡 Hint

Think about how AUTO_INCREMENT keeps track internally.

📝 Syntax
advanced
1:30remaining
Which statement correctly resets AUTO_INCREMENT to 1?

You want to reset the AUTO_INCREMENT counter of a table named orders to 1. Which SQL statement is correct?

AALTER TABLE orders AUTO_INCREMENT = 1;
BUPDATE orders SET AUTO_INCREMENT = 1;
CRESET AUTO_INCREMENT orders TO 1;
DSET orders.AUTO_INCREMENT = 1;
Attempts:
2 left
💡 Hint

Look for the correct syntax to alter table properties.

optimization
advanced
2:00remaining
How to avoid gaps in AUTO_INCREMENT after deletes?

You want to keep AUTO_INCREMENT values continuous without gaps after deleting rows. Which approach is best?

AUse RESTART IDENTITY in DELETE statements
BUse transactions to rollback deletes
CManually update AUTO_INCREMENT value after deletes
DDo not delete rows; mark them as inactive instead
Attempts:
2 left
💡 Hint

Think about how AUTO_INCREMENT works and what causes gaps.

🔧 Debug
expert
2:30remaining
Why does this insert fail with duplicate key error?

Given this table and inserts:

CREATE TABLE products (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20));
INSERT INTO products (id, name) VALUES (1, 'Pen');
INSERT INTO products (name) VALUES ('Pencil');
INSERT INTO products (id, name) VALUES (1, 'Eraser');

Why does the last insert fail?

MySQL
CREATE TABLE products (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20));
INSERT INTO products (id, name) VALUES (1, 'Pen');
INSERT INTO products (name) VALUES ('Pencil');
INSERT INTO products (id, name) VALUES (1, 'Eraser');
ABecause AUTO_INCREMENT cannot be used with explicit id values
BBecause 'Eraser' is a duplicate name
CBecause id 1 already exists, causing duplicate primary key error
DBecause the table does not allow inserts without id
Attempts:
2 left
💡 Hint

Check the primary key constraints and inserted values.