0
0
SQLquery~20 mins

UPDATE with subquery preview in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of UPDATE with correlated subquery

Consider the tables employees and departments. The employees table has columns id, name, salary, and department_id. The departments table has columns id and budget.

What will be the salary of employee with id = 3 after running this query?

SQL
UPDATE employees e
SET salary = (
  SELECT budget / 10
  FROM departments d
  WHERE d.id = e.department_id
)
WHERE e.id = 3;
A5000
B10000
CNULL
DNo change (salary remains the same)
Attempts:
2 left
💡 Hint

Think about how the subquery uses the department_id of the employee to find the department's budget.

🧠 Conceptual
intermediate
1:30remaining
Understanding UPDATE with subquery behavior

Which statement best describes what happens when you run an UPDATE statement with a subquery in the SET clause?

AThe subquery is ignored if it returns more than one row.
BThe subquery is executed only once before the update starts and its result is used for all rows.
CThe subquery is executed once for each row being updated, using values from that row.
DThe subquery must not reference the table being updated.
Attempts:
2 left
💡 Hint

Think about how the subquery can use values from the row being updated.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in UPDATE with subquery

Which option contains a syntax error in the UPDATE statement using a subquery?

SQL
UPDATE products p
SET price = (SELECT AVG(price) FROM products WHERE category = p.category)
WHERE category = 'Electronics';
AUPDATE products SET price = (SELECT AVG(price) FROM products WHERE category = products.category) WHERE category = 'Electronics';
BUPDATE products p SET price = SELECT AVG(price) FROM products WHERE category = p.category WHERE category = 'Electronics';
CUPDATE products p SET price = (SELECT AVG(price) FROM products WHERE category = p.category);
DUPDATE products p SET price = (SELECT AVG(price) FROM products WHERE category = p.category) WHERE category = 'Electronics';
Attempts:
2 left
💡 Hint

Look carefully at the placement of parentheses and keywords.

optimization
advanced
2:30remaining
Optimizing UPDATE with subquery for performance

You want to update the salary of all employees to the average salary of their department. Which query is more efficient?

AUPDATE employees e SET salary = (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id);
BUPDATE employees SET salary = (SELECT AVG(salary) FROM employees);
CUPDATE employees SET salary = (SELECT salary FROM employees WHERE id = employees.id);
DUPDATE employees SET salary = avg_salary FROM (SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id) d WHERE employees.department_id = d.department_id;
Attempts:
2 left
💡 Hint

Consider how many times the subquery runs in each option.

🔧 Debug
expert
3:00remaining
Why does this UPDATE with subquery cause an error?

Given the query below, why does it cause an error?

SQL
UPDATE orders o
SET total = (SELECT SUM(amount) FROM order_items WHERE order_id = o.id)
WHERE o.id IN (SELECT id FROM orders WHERE status = 'pending');
AThe subquery is valid and causes no error.
BThe subquery returns multiple rows causing a runtime error.
CThe subquery returns NULL for some rows causing the update to fail.
DThe subquery references the same table being updated causing a conflict.
Attempts:
2 left
💡 Hint

Think about whether the subquery returns a single value per row.