Bird
Raised Fist0
SQLquery~20 mins

AUTO_INCREMENT behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
AUTO_INCREMENT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2: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;
A
1, Alice
2, Bob
3, Charlie
4, David
B
1, Alice
3, Charlie
4, David
C
1, Alice
2, David
3, Charlie
D
1, Alice
3, Charlie
2, David
Attempts:
2 left
💡 Hint
AUTO_INCREMENT does not reuse deleted IDs by default.
🧠 Conceptual
intermediate
1: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?
AThe AUTO_INCREMENT counter continues from the highest existing ID plus one.
BThe AUTO_INCREMENT counter starts from the last inserted ID, even if rows were deleted.
CThe AUTO_INCREMENT counter is lost and must be manually reset.
DThe AUTO_INCREMENT counter resets to 1 regardless of existing rows.
Attempts:
2 left
💡 Hint
Think about how the server remembers the last used ID.
📝 Syntax
advanced
2: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?
ACREATE TABLE orders (order_id INT AUTO_INCREMENT PRIMARY KEY START WITH 1000);
BCREATE TABLE orders (order_id INT PRIMARY KEY AUTO_INCREMENT=1000);
CCREATE TABLE orders (order_id INT AUTO_INCREMENT PRIMARY KEY) AUTO_INCREMENT=1000;
DCREATE TABLE orders (order_id INT AUTO_INCREMENT PRIMARY KEY) START=1000;
Attempts:
2 left
💡 Hint
The AUTO_INCREMENT starting value is set outside the column definition.
🔧 Debug
advanced
2: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');
ABecause the name 'Pen' is a duplicate and violates a unique constraint.
BBecause AUTO_INCREMENT columns cannot be explicitly assigned values.
CBecause the table does not allow inserting any rows after the first.
DBecause the value 1 for product_id already exists from the first insert.
Attempts:
2 left
💡 Hint
Check the existing IDs before the insert.
❓ optimization
expert
3: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?
ADisable autocommit and insert all rows in a single transaction, then commit once.
BInsert rows one by one with autocommit enabled to ensure each ID is assigned sequentially.
CManually assign IDs instead of using AUTO_INCREMENT to control gaps.
DDelete rows after insert to fill gaps in AUTO_INCREMENT IDs.
Attempts:
2 left
💡 Hint
Think about how transactions affect AUTO_INCREMENT values.

Practice

(1/5)
1. What does the AUTO_INCREMENT attribute do in a SQL table?
easy
A. Encrypts the data in the column
B. Deletes duplicate rows automatically
C. Automatically assigns a unique number to new rows
D. Prevents any new rows from being added

Solution

  1. Step 1: Understand the purpose of AUTO_INCREMENT

    AUTO_INCREMENT is used to generate a unique number for each new row automatically, usually for primary keys.
  2. Step 2: Compare options with this behavior

    Only Automatically assigns a unique number to new rows describes this behavior correctly; others describe unrelated actions.
  3. Final Answer:

    Automatically assigns a unique number to new rows -> Option C
  4. Quick Check:

    AUTO_INCREMENT = unique number assignment [OK]
Hint: AUTO_INCREMENT means unique numbers auto-assigned [OK]
Common Mistakes:
  • Thinking AUTO_INCREMENT deletes duplicates
  • Confusing AUTO_INCREMENT with encryption
  • Assuming AUTO_INCREMENT blocks inserts
2. Which of the following is the correct syntax to create a table with an AUTO_INCREMENT column in SQL?
easy
A. CREATE TABLE users (id INT INCREMENT_AUTO, name VARCHAR(50), PRIMARY KEY(id));
B. CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(id));
C. CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY, name VARCHAR(50));
D. CREATE TABLE users (id INT AUTO_INCREMENTED, name VARCHAR(50), PRIMARY KEY(id));

Solution

  1. Step 1: Review correct AUTO_INCREMENT syntax

    The correct syntax is to declare the column as INT AUTO_INCREMENT and set it as PRIMARY KEY.
  2. Step 2: Check each option for syntax errors

    CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(id)); matches the correct syntax. Options B, C, and D use invalid keywords or misplaced PRIMARY KEY.
  3. Final Answer:

    CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(id)); -> Option B
  4. Quick Check:

    Correct AUTO_INCREMENT syntax = CREATE TABLE users (id INT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(id)); [OK]
Hint: AUTO_INCREMENT goes with INT and PRIMARY KEY [OK]
Common Mistakes:
  • Using wrong keywords like INCREMENT_AUTO
  • Placing PRIMARY KEY inside column definition incorrectly
  • Adding suffixes like AUTO_INCREMENTED
3. Given the table products with id INT AUTO_INCREMENT PRIMARY KEY and name VARCHAR(50), what will be the id values after these inserts?
INSERT INTO products (name) VALUES ('Pen');
INSERT INTO products (name) VALUES ('Pencil');
INSERT INTO products (name) VALUES ('Eraser');
medium
A. [1, 2, 3]
B. [0, 1, 2]
C. [NULL, NULL, NULL]
D. [1, 1, 1]

Solution

  1. Step 1: Understand AUTO_INCREMENT numbering

    Each insert without specifying id gets the next unique number starting from 1 by default.
  2. Step 2: Apply to the three inserts

    First insert gets 1, second gets 2, third gets 3 as id values.
  3. Final Answer:

    [1, 2, 3] -> Option A
  4. Quick Check:

    Sequential AUTO_INCREMENT IDs = [1, 2, 3] [OK]
Hint: AUTO_INCREMENT starts at 1 and increments by 1 [OK]
Common Mistakes:
  • Assuming IDs start at 0
  • Expecting NULL values for AUTO_INCREMENT
  • Thinking IDs repeat for each insert
4. You have a table orders with order_id INT AUTO_INCREMENT PRIMARY KEY. You try to insert a row with order_id = 0 manually, but get an error. What is the likely cause?
medium
A. You cannot insert a value manually into an AUTO_INCREMENT column without special syntax
B. The table does not allow any inserts
C. The column is not set as PRIMARY KEY
D. The AUTO_INCREMENT value must be a string

Solution

  1. Step 1: Understand manual inserts on AUTO_INCREMENT columns

    By default, inserting a value manually into an AUTO_INCREMENT column can cause errors unless allowed explicitly.
  2. Step 2: Analyze the error cause

    The error likely occurs because the database expects to manage the AUTO_INCREMENT values automatically, and manual insertion conflicts with this.
  3. Final Answer:

    You cannot insert a value manually into an AUTO_INCREMENT column without special syntax -> Option A
  4. Quick Check:

    Manual insert on AUTO_INCREMENT needs care = You cannot insert a value manually into an AUTO_INCREMENT column without special syntax [OK]
Hint: AUTO_INCREMENT columns usually reject manual values [OK]
Common Mistakes:
  • Assuming inserts are blocked entirely
  • Confusing PRIMARY KEY with AUTO_INCREMENT behavior
  • Thinking AUTO_INCREMENT accepts strings
5. A table employees has emp_id INT AUTO_INCREMENT PRIMARY KEY. After deleting the row with emp_id = 3, you insert a new row without specifying emp_id. What will be the emp_id of the new row?
hard
A. 3
B. NULL
C. 1
D. The next highest number after the last inserted ID

Solution

  1. Step 1: Understand AUTO_INCREMENT behavior after deletions

    Deleting a row does not reset or reuse the deleted AUTO_INCREMENT value by default.
  2. Step 2: Determine the new ID assigned

    The new row gets the next number after the highest previously assigned ID, not the deleted one.
  3. Final Answer:

    The next highest number after the last inserted ID -> Option D
  4. Quick Check:

    Deleted IDs are not reused automatically = The next highest number after the last inserted ID [OK]
Hint: AUTO_INCREMENT never reuses deleted IDs by default [OK]
Common Mistakes:
  • Assuming deleted IDs get reused automatically
  • Thinking new inserts reset to 1
  • Expecting NULL for AUTO_INCREMENT columns
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