0
0
SQLquery~30 mins

UPDATE with subquery preview in SQL - Mini Project: Build & Apply

Choose your learning style9 modes available
UPDATE with subquery preview
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use SELECT to show the updated table content.