0
0
MySQLquery~10 mins

ON DUPLICATE KEY UPDATE in MySQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a new user or update the email if the user ID already exists.

MySQL
INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com') [1];
Drag options to blanks, or click blank then click option'
AWHERE id = 1
BSET email = 'alice@example.com'
CON DUPLICATE KEY UPDATE email = 'alice@example.com'
DUPDATE users SET email = 'alice@example.com'
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE clause after INSERT
Trying to use SET without ON DUPLICATE KEY UPDATE
Writing UPDATE statement instead of INSERT
2fill in blank
medium

Complete the code to update the user's name to 'Bob' if the ID already exists.

MySQL
INSERT INTO users (id, name) VALUES (2, 'Bob') [1];
Drag options to blanks, or click blank then click option'
AON DUPLICATE KEY UPDATE name = 'Bob'
BON DUPLICATE KEY UPDATE id = 2
CWHERE id = 2
DSET name = 'Bob'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to update the ID column
Using WHERE clause incorrectly
Omitting the ON DUPLICATE KEY UPDATE clause
3fill in blank
hard

Fix the error in the code to update the email to 'bob@example.com' on duplicate key.

MySQL
INSERT INTO users (id, email) VALUES (2, 'bob@example.com') [1];
Drag options to blanks, or click blank then click option'
AON DUPLICATE UPDATE email = 'bob@example.com'
BON DUPLICATE KEY UPDATE email = 'bob@example.com'
CON DUPLICATE KEY SET email = 'bob@example.com'
DON DUPLICATE KEY WHERE email = 'bob@example.com'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SET instead of UPDATE
Omitting KEY in the clause
Using WHERE clause incorrectly
4fill in blank
hard

Fill both blanks to insert a product or update its price and stock if the product ID exists.

MySQL
INSERT INTO products (id, price, stock) VALUES ([1], 100, 50) [2] price = 100, stock = 50;
Drag options to blanks, or click blank then click option'
A3
BON DUPLICATE KEY UPDATE
C5
DWHERE id = 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE clause instead of ON DUPLICATE KEY UPDATE
Putting wrong ID value
Omitting the update clause
5fill in blank
hard

Fill all three blanks to insert or update a user's name, email, and age on duplicate key.

MySQL
INSERT INTO users (id, name, email, age) VALUES ([1], [2], [3], 30) ON DUPLICATE KEY UPDATE name = [2], email = [3], age = 30;
Drag options to blanks, or click blank then click option'
A10
B'Charlie'
C'charlie@example.com'
D'Charlie Brown'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different values in insert and update parts
Omitting quotes around strings
Mixing up name and email positions