The UPDATE statement changes existing data in a table by modifying one or more columns for rows that match a condition.
Click to reveal answer
beginner
How can you use expressions in an UPDATE statement?
You can use expressions to calculate new values based on existing column values, like adding 10 to a salary: SET salary = salary + 10.
Click to reveal answer
beginner
Example: What does this SQL do? UPDATE employees SET salary = salary * 1.05 WHERE department = 'Sales';
It increases the salary by 5% for all employees in the Sales department.
Click to reveal answer
intermediate
Can you use functions in UPDATE expressions? Give an example.
Yes, you can use functions. For example, to set a date column to the current date: UPDATE orders SET shipped_date = CURRENT_DATE WHERE shipped_date IS NULL;
Click to reveal answer
beginner
Why is it important to use a WHERE clause in an UPDATE statement with expressions?
Without a WHERE clause, the UPDATE will change all rows in the table, which might cause unintended data changes.
Click to reveal answer
What happens if you run an UPDATE statement without a WHERE clause?
AOnly the first row is updated
BNo rows are updated
CAll rows in the table are updated
DThe statement will cause an error
✗ Incorrect
Without a WHERE clause, the UPDATE affects every row in the table.
Which of the following is a valid expression in an UPDATE statement?
ASET price = price - 'ten'
BSET price = price + 10
CSET price = price & 10
DSET price = 'price + 10'
✗ Incorrect
You can add numbers directly in expressions like price = price + 10. The others are invalid or use wrong syntax.
How do you increase a numeric column 'score' by 20% using UPDATE?
ASET score = score * 1.2
BSET score = score + 20
CSET score = score / 1.2
DSET score = score - 20
✗ Incorrect
Multiplying by 1.2 increases the value by 20%.
Which SQL function can you use to set a date column to today’s date in an UPDATE?
ACURRENT_DATE
BNOW()
CGETDATE()
DTODAY()
✗ Incorrect
CURRENT_DATE is standard SQL for today's date. NOW() and GETDATE() are specific to some databases; TODAY() is not standard.
What does this statement do? UPDATE products SET stock = stock - 1 WHERE product_id = 100;
ADeletes product 100
BSets stock to 1 for product 100
CIncreases stock by 1 for product 100
DDecreases stock by 1 for product 100
✗ Incorrect
It subtracts 1 from the stock column for the product with ID 100.
Explain how to use expressions in an UPDATE statement to modify data.
Think about changing values based on current data.
You got /4 concepts.
Describe why the WHERE clause is important in UPDATE statements with expressions.
Consider what happens if you forget WHERE.
You got /4 concepts.
Practice
(1/5)
1. What does the SQL statement UPDATE employees SET salary = salary + 500 do?
easy
A. Sets all salaries to 500
B. Increases the salary of all employees by 500
C. Deletes employees with salary 500
D. Creates a new employee with salary 500
Solution
Step 1: Understand the UPDATE statement
The statement updates the 'salary' column in the 'employees' table.
Step 2: Analyze the expression used
It adds 500 to the current salary value for every row because no WHERE clause limits the rows.
Final Answer:
Increases the salary of all employees by 500 -> Option B
Quick Check:
salary = salary + 500 [OK]
Hint: No WHERE means update all rows with the expression [OK]
Common Mistakes:
Thinking it sets salary to 500 instead of adding
Confusing UPDATE with DELETE
Assuming it adds 500 only to some rows without WHERE
2. Which of the following is the correct syntax to increase the price by 10% only for products in category 'Books'?
easy
A. UPDATE products SET price = price * 1.10 WHERE category = 'Books';
B. UPDATE products SET price = price + 10% WHERE category = 'Books';
C. UPDATE products SET price = price + 0.10 WHERE category = 'Books';
D. UPDATE products SET price = price * 10 WHERE category = 'Books';
Solution
Step 1: Identify correct expression for 10% increase
Multiplying price by 1.10 increases it by 10%.
Step 2: Check WHERE clause for category filtering
WHERE category = 'Books' limits update to only books.
Final Answer:
UPDATE products SET price = price * 1.10 WHERE category = 'Books'; -> Option A
Quick Check:
price * 1.10 with WHERE category = 'Books' [OK]
Hint: Use multiplication by 1.10 for 10% increase [OK]
Common Mistakes:
Using + 10% which is invalid syntax
Adding 0.10 instead of 10%
Multiplying price by 10 instead of 1.10
3. Given the table accounts with columns id, balance, what will be the balances after running: UPDATE accounts SET balance = balance - 100 WHERE balance >= 500; Initial balances: 400, 500, 600
medium
A. [400, 600, 700]
B. [300, 400, 500]
C. [400, 500, 600]
D. [400, 400, 500]
Solution
Step 1: Identify which rows meet the WHERE condition
Only balances 500 and 600 are >= 500, so only these rows update.