0
0
MySQLquery~20 mins

ON DUPLICATE KEY UPDATE in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ON DUPLICATE KEY UPDATE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of ON DUPLICATE KEY UPDATE on existing row

Given the table users with columns id (primary key) and score, what will be the content of the table after executing the following query?

INSERT INTO users (id, score) VALUES (1, 10) ON DUPLICATE KEY UPDATE score = score + 5;

Assume the table initially contains: {id: 1, score: 20}.

A{id: 1, score: 10}
B{id: 1, score: 25}
C{id: 1, score: 15}
D{id: 1, score: 20}
Attempts:
2 left
💡 Hint

Think about what happens when the primary key already exists.

🧠 Conceptual
intermediate
2:00remaining
Behavior when no duplicate key exists

What happens when you run this query on a table where the primary key does not exist yet?

INSERT INTO products (product_id, quantity) VALUES (5, 100) ON DUPLICATE KEY UPDATE quantity = quantity + 10;
AThe query fails with a duplicate key error.
BThe existing row with product_id 5 is updated to quantity 110.
CA new row with product_id 5 and quantity 100 is inserted.
DNo changes happen to the table.
Attempts:
2 left
💡 Hint

Consider what happens if the key is not found.

📝 Syntax
advanced
2:00remaining
Correct syntax for ON DUPLICATE KEY UPDATE

Which of the following queries is syntactically correct in MySQL?

INSERT INTO inventory (item_id, stock) VALUES (3, 50) ON DUPLICATE KEY UPDATE stock = stock - 1;
AINSERT INTO inventory (item_id, stock) VALUES (3, 50) ON DUPLICATE UPDATE stock = stock - 1;
BINSERT INTO inventory (item_id, stock) VALUES (3, 50) DUPLICATE KEY UPDATE stock = stock - 1;
CINSERT INTO inventory (item_id, stock) VALUES (3, 50) ON DUPLICATE KEY SET stock = stock - 1;
DINSERT INTO inventory (item_id, stock) VALUES (3, 50) ON DUPLICATE KEY UPDATE stock = stock - 1;
Attempts:
2 left
💡 Hint

Check the exact keywords and their order.

optimization
advanced
2:00remaining
Optimizing multiple row inserts with ON DUPLICATE KEY UPDATE

You want to insert multiple rows into a table and update existing rows if keys duplicate. Which query is the most efficient?

AUse a single multi-row INSERT ... ON DUPLICATE KEY UPDATE query with all rows.
BRun multiple single-row INSERT ... ON DUPLICATE KEY UPDATE queries, one per row.
CDelete all rows first, then insert all rows without ON DUPLICATE KEY UPDATE.
DUse UPDATE queries for existing rows and INSERT for new rows separately.
Attempts:
2 left
💡 Hint

Think about reducing the number of queries sent to the database.

🔧 Debug
expert
3:00remaining
Diagnosing unexpected behavior with ON DUPLICATE KEY UPDATE

Consider this table orders with a unique key on order_number. You run:

INSERT INTO orders (order_number, status) VALUES ('A123', 'pending') ON DUPLICATE KEY UPDATE status = 'pending';

But the status does not update when you expect it to. What is the most likely cause?

AThe existing row already has status 'pending', so no change occurs.
BThe query syntax is incorrect and causes a silent failure.
CThe database does not support ON DUPLICATE KEY UPDATE.
DThe unique key is not on order_number, so no duplicate is detected.
Attempts:
2 left
💡 Hint

Think about what happens if the update sets the same value as before.