0
0
SQLquery~20 mins

UPDATE trigger with OLD and NEW in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after updating a row with this trigger?
Consider a table employees with columns id, salary, and bonus. There is an UPDATE trigger that sets NEW.bonus = OLD.salary * 0.1 before updating the row. If the row with id = 1 has salary = 1000 and bonus = 0, what will be the bonus value after running UPDATE employees SET salary = 1200 WHERE id = 1;?
SQL
CREATE TRIGGER update_bonus BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
  SET NEW.bonus = OLD.salary * 0.1;
END;
Abonus = 0
Bbonus = 120
Cbonus = 100
Dbonus = NULL
Attempts:
2 left
💡 Hint
Remember that OLD refers to the row before update, NEW is the row after update but before saving.
🧠 Conceptual
intermediate
1:30remaining
Which statement about OLD and NEW in UPDATE triggers is true?
In an UPDATE trigger, what do OLD and NEW represent?
AOLD is the row after update; NEW is the row before update
BOLD is the row before update; NEW is the row after update but before saving
COLD and NEW both represent the row after update
DOLD and NEW both represent the row before update
Attempts:
2 left
💡 Hint
Think about what you want to compare or change during an update.
📝 Syntax
advanced
2:00remaining
Which trigger code will cause a syntax error?
Identify the option that will cause a syntax error when creating an UPDATE trigger using OLD and NEW.
ACREATE TRIGGER trg BEFORE UPDATE ON sales FOR EACH ROW BEGIN SET NEW.total = OLD.amount + NEW.tax; END;
BCREATE TRIGGER trg BEFORE UPDATE ON sales FOR EACH ROW BEGIN SET NEW.total = OLD.amount + OLD.tax; END;
CCREATE TRIGGER trg BEFORE UPDATE ON sales FOR EACH ROW BEGIN SET NEW.total = NEW.amount + OLD.tax; END;
DCREATE TRIGGER trg BEFORE UPDATE ON sales FOR EACH ROW BEGIN SET OLD.total = NEW.amount + OLD.tax; END;
Attempts:
2 left
💡 Hint
Can you assign a value to OLD in a trigger?
optimization
advanced
2:30remaining
How to optimize an UPDATE trigger that recalculates a column only if a specific column changes?
You have an UPDATE trigger that recalculates the discount column whenever any column changes. How can you optimize it to recalculate only if price changes?
AAdd an IF statement: IF NEW.price <> OLD.price THEN recalculate discount END IF;
BAlways recalculate discount without any condition for safety.
CUse a BEFORE INSERT trigger instead of UPDATE trigger.
DRemove the trigger and calculate discount in application code.
Attempts:
2 left
💡 Hint
Check if the value changed before recalculating.
🔧 Debug
expert
3:00remaining
Why does this UPDATE trigger cause an infinite loop?
Given this trigger code, why does updating the quantity column cause an infinite loop? CREATE TRIGGER trg_update_quantity AFTER UPDATE ON inventory FOR EACH ROW BEGIN UPDATE inventory SET quantity = NEW.quantity WHERE id = NEW.id; END;
SQL
CREATE TRIGGER trg_update_quantity AFTER UPDATE ON inventory
FOR EACH ROW
BEGIN
  UPDATE inventory SET quantity = NEW.quantity WHERE id = NEW.id;
END;
AThe trigger updates the same table causing it to fire repeatedly without stop
BThe trigger uses AFTER UPDATE instead of BEFORE UPDATE
CThe trigger tries to update OLD values which is not allowed
DThe trigger does not have a WHERE clause to limit updates
Attempts:
2 left
💡 Hint
Think about what happens when a trigger updates the same table it listens to.