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?
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
Check which employees belong to the 'Sales' department and how their salary changes.
The employee with id=3 is in department 1, which is 'Sales'. The query adds 1000 to their salary of 5500, resulting in 6500.
Choose the correct syntax to update a table orders by joining with customers to set orders.status to 'shipped' where customers.region is 'West'.
MySQL uses JOIN syntax directly in UPDATE, not FROM.
Option D uses the correct MySQL syntax with JOIN in UPDATE. Option D uses invalid FROM clause. Option D uses comma syntax instead of JOIN. Option D uses subquery, not JOIN.
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?
Indexes help speed up JOIN and WHERE conditions.
Adding indexes on the join and filter columns helps MySQL quickly find matching rows, improving update speed. Other options either reduce precision or do not improve performance.
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?
Check which table 'region' belongs to and how it is referenced.
The WHERE clause references 'region' without specifying the table alias. Since 'region' is in customers table, it must be referenced as 'c.region'. Without prefix, MySQL looks for 'region' in orders table and fails.
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;
Think about how JOIN and WHERE filter rows for update.
The JOIN matches students with their scores. The WHERE filters scores >= 60. So students with at least one such score get updated. It does not check all scores.