📖 Scenario: You manage a small bookstore database. You want to update the prices of some books based on their current prices.
🎯 Goal: Build an UPDATE SQL query that uses a subquery to preview the new prices before applying the update.
📋 What You'll Learn
Create a table called books with columns id, title, and price.
Insert 3 books with exact titles and prices.
Create a subquery that selects the id and the new price (current price + 5).
Write an UPDATE statement that uses the subquery to update the prices.
💡 Why This Matters
🌍 Real World
Updating prices in a product database based on calculated values is common in retail and inventory management.
💼 Career
Knowing how to use UPDATE with subqueries helps database administrators and developers maintain accurate and dynamic data.
Progress0 / 4 steps
1
Create the books table and insert data
Create a table called books with columns id (integer), title (text), and price (integer). Then insert these exact rows: (1, 'Book A', 10), (2, 'Book B', 15), (3, 'Book C', 20).
SQL
Hint
Use CREATE TABLE to make the table and INSERT INTO to add rows.
2
Write a subquery to preview new prices
Create a subquery called price_preview that selects id and calculates a new price as price + 5 from the books table.
SQL
Hint
Use a WITH clause to create the subquery named price_preview.
3
Write the UPDATE statement using the subquery
Write an UPDATE statement that updates the price in books by joining with the price_preview subquery on id, setting price to new_price.
SQL
Hint
Use UPDATE with FROM to join the subquery and set the new prices.
4
Complete the query with a preview SELECT
Add a SELECT statement after the UPDATE to preview the updated id, title, and price from the books table.
SQL
Hint
Use SELECT to show the updated table content.
Practice
(1/5)
1. What does an UPDATE statement with a subquery do in SQL?
easy
A. It changes values in a table based on data from another table.
B. It deletes rows from a table using a subquery.
C. It creates a new table from a subquery.
D. It selects rows without changing any data.
Solution
Step 1: Understand the purpose of UPDATE with subquery
UPDATE modifies existing rows in a table. When combined with a subquery, it uses data from another table to decide new values.
Step 2: Identify the effect of the subquery
The subquery provides values to update the target table, ensuring related data stays consistent.
Final Answer:
It changes values in a table based on data from another table. -> Option A
Quick Check:
UPDATE with subquery = change data using other table [OK]
Hint: UPDATE with subquery changes data using another table's values [OK]
Common Mistakes:
Thinking UPDATE deletes rows
Confusing UPDATE with SELECT
Believing UPDATE creates new tables
2. Which of the following is the correct syntax for updating a table using a subquery in SQL?
easy
A. UPDATE table1 SET (col1) = (SELECT col2 FROM table2);
B. UPDATE table1 FROM (SELECT col2 FROM table2) SET col1 = col2;
C. UPDATE table1 SET col1 = SELECT col2 FROM table2 WHERE id = table1.id;
D. UPDATE table1 SET col1 = (SELECT col2 FROM table2 WHERE id = table1.id);
Solution
Step 1: Review correct UPDATE with subquery syntax
The correct syntax uses SET column = (SELECT ...), with the subquery in parentheses returning a single value.
Step 2: Check each option
UPDATE table1 SET col1 = (SELECT col2 FROM table2 WHERE id = table1.id); correctly uses SET col1 = (SELECT ... WHERE ...). Options B, C, and D have syntax errors or missing parentheses.
Final Answer:
UPDATE table1 SET col1 = (SELECT col2 FROM table2 WHERE id = table1.id); -> Option D
Quick Check:
SET col = (SELECT ...) is correct syntax [OK]
Hint: Use SET col = (SELECT ...) with parentheses for subquery [OK]
Thinking it sets salary to bonus instead of adding
Confusing UPDATE with DELETE
4. Identify the error in this SQL statement:
UPDATE Products SET price = (SELECT discount FROM Discounts WHERE product_id = Products.id);
Assuming Discounts has multiple rows per product_id.
medium
A. Subquery returns multiple rows causing an error.
B. Missing WHERE clause in UPDATE.
C. Incorrect table alias usage.
D. UPDATE cannot use subqueries.
Solution
Step 1: Check subquery result for each row
The subquery selects discount from Discounts where product_id matches. If multiple discounts exist, it returns multiple rows.
Step 2: Understand UPDATE with subquery requirement
UPDATE expects the subquery to return a single value per row. Multiple rows cause an error.
Final Answer:
Subquery returns multiple rows causing an error. -> Option A
Quick Check:
Subquery must return one value per row [OK]
Hint: Subquery must return single value per row to avoid error [OK]
Common Mistakes:
Ignoring multiple rows returned by subquery
Assuming missing WHERE causes error here
Believing UPDATE disallows subqueries
5. You want to update a Users table to set each user's status to 'VIP' if their total orders amount exceeds 1000. The Orders table has user_id and amount. Which query correctly uses UPDATE with a subquery to do this?
hard
A. UPDATE Users SET status = 'VIP' WHERE (SELECT amount FROM Orders WHERE user_id = Users.id) > 1000;
B. UPDATE Users SET status = 'VIP' WHERE EXISTS (SELECT 1 FROM Orders WHERE user_id = Users.id AND amount > 1000);
C. UPDATE Users SET status = 'VIP' WHERE id IN (SELECT user_id FROM Orders GROUP BY user_id HAVING SUM(amount) > 1000);
D. UPDATE Users SET status = (SELECT 'VIP' FROM Orders WHERE user_id = Users.id AND amount > 1000);
Solution
Step 1: Understand the requirement
We must update Users whose total order amount exceeds 1000. This requires grouping Orders by user_id and summing amounts.
Step 2: Analyze each option
UPDATE Users SET status = 'VIP' WHERE (SELECT amount FROM Orders WHERE user_id = Users.id) > 1000; uses a subquery in WHERE that returns multiple rows if user has multiple orders, causing an error. UPDATE Users SET status = (SELECT 'VIP' FROM Orders WHERE user_id = Users.id AND amount > 1000); tries to set status from subquery returning 'VIP' per order, which is incorrect. UPDATE Users SET status = 'VIP' WHERE EXISTS (SELECT 1 FROM Orders WHERE user_id = Users.id AND amount > 1000); checks if any order > 1000, not total sum. UPDATE Users SET status = 'VIP' WHERE id IN (SELECT user_id FROM Orders GROUP BY user_id HAVING SUM(amount) > 1000); correctly uses WHERE id IN with grouped subquery filtering users with sum(amount) > 1000.
Final Answer:
UPDATE Users SET status = 'VIP' WHERE id IN (SELECT user_id FROM Orders GROUP BY user_id HAVING SUM(amount) > 1000); -> Option C
Quick Check:
Use GROUP BY and HAVING in subquery for totals [OK]
Hint: Use GROUP BY and HAVING in subquery for conditional UPDATE [OK]
Common Mistakes:
Using WHERE EXISTS without aggregation
Setting column from subquery returning multiple rows
Ignoring grouping for total sums
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.