0
0
MySQLquery~20 mins

UPDATE with JOIN in MySQL - Practice Problems & Coding Challenges

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

Given two tables, employees and departments, the query updates employee salaries by adding 1000 for those in the 'Sales' department. What will be the salary of employee with id=3 after running this?

MySQL
UPDATE employees e
JOIN departments d ON e.department_id = d.id
SET e.salary = e.salary + 1000
WHERE d.name = 'Sales';

-- employees table before update:
-- id | name    | department_id | salary
-- 1  | Alice   | 1             | 5000
-- 2  | Bob     | 2             | 6000
-- 3  | Charlie | 1             | 5500

-- departments table:
-- id | name
-- 1  | Sales
-- 2  | HR
A5500
B6500
C6000
DSyntaxError
Attempts:
2 left
💡 Hint

Check which employees belong to the 'Sales' department and how their salary changes.

📝 Syntax
intermediate
2:00remaining
Which UPDATE with JOIN syntax is correct in MySQL?

Choose the correct syntax to update a table orders by joining with customers to set orders.status to 'shipped' where customers.region is 'West'.

AUPDATE orders SET status = 'shipped' WHERE customer_id IN (SELECT id FROM customers WHERE region = 'West');
BUPDATE orders SET status = 'shipped' FROM orders o JOIN customers c ON o.customer_id = c.id WHERE c.region = 'West';
CUPDATE orders o, customers c SET o.status = 'shipped' WHERE o.customer_id = c.id AND c.region = 'West';
DUPDATE orders o JOIN customers c ON o.customer_id = c.id SET status = 'shipped' WHERE c.region = 'West';
Attempts:
2 left
💡 Hint

MySQL uses JOIN syntax directly in UPDATE, not FROM.

optimization
advanced
2:00remaining
How to optimize this UPDATE with JOIN for large tables?

You have a large products table and categories table. You want to update products.discount to 10 where categories.name is 'Clearance'. The current query is:

UPDATE products p
JOIN categories c ON p.category_id = c.id
SET p.discount = 10
WHERE c.name = 'Clearance';

Which optimization is best to improve performance?

AAdd an index on categories.name and products.category_id columns.
BRewrite the query using a subquery instead of JOIN.
CRemove the WHERE clause to update all products.
DUse a LEFT JOIN instead of JOIN.
Attempts:
2 left
💡 Hint

Indexes help speed up JOIN and WHERE conditions.

🔧 Debug
advanced
2:00remaining
Why does this UPDATE with JOIN fail with 'Unknown column' error?

Consider this query:

UPDATE orders o
JOIN customers c ON o.customer_id = c.id
SET status = 'delivered'
WHERE region = 'East';

It raises an error: Unknown column 'region' in 'where clause'. Why?

ABecause 'region' column does not exist in orders table and is not prefixed with 'c.'.
BBecause 'region' is ambiguous and needs table alias prefix.
CBecause JOIN syntax is invalid in UPDATE.
DBecause SET clause must come after WHERE clause.
Attempts:
2 left
💡 Hint

Check which table 'region' belongs to and how it is referenced.

🧠 Conceptual
expert
2:00remaining
What is the effect of this UPDATE with JOIN query?

Given tables students and scores, what does this query do?

UPDATE students s
JOIN scores sc ON s.id = sc.student_id
SET s.status = 'passed'
WHERE sc.score >= 60;
ARaises an error because UPDATE with JOIN cannot use WHERE on joined table.
BMarks all students as 'passed' regardless of scores.
CMarks students as 'passed' if they have at least one score 60 or above.
DMarks students as 'passed' only if all their scores are 60 or above.
Attempts:
2 left
💡 Hint

Think about how JOIN and WHERE filter rows for update.