0
0
SQLquery~20 mins

AFTER trigger execution in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AFTER 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 an AFTER INSERT trigger?

Consider a table Orders with columns OrderID and Status. An AFTER INSERT trigger updates the Status to 'Processed' after a new order is inserted.

What will be the Status of the newly inserted order immediately after the insert completes?

SQL
CREATE TABLE Orders (OrderID INT PRIMARY KEY, Status VARCHAR(20));

CREATE TRIGGER trg_AfterInsert
AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
  UPDATE Orders SET Status = 'Processed' WHERE OrderID = NEW.OrderID;
END;

INSERT INTO Orders (OrderID, Status) VALUES (1, 'Pending');

SELECT Status FROM Orders WHERE OrderID = 1;
A'Processed'
B'Pending'
CNULL
DTrigger causes error, no output
Attempts:
2 left
💡 Hint

Think about when AFTER triggers run in relation to the insert operation.

🧠 Conceptual
intermediate
1:30remaining
When does an AFTER UPDATE trigger execute?

Choose the correct statement about when an AFTER UPDATE trigger runs in a database.

AAfter the update operation modifies the row
BBefore the update operation modifies the row
COnly if the update operation fails
DBefore the update operation starts scanning rows
Attempts:
2 left
💡 Hint

AFTER triggers run after the triggering event completes.

🔧 Debug
advanced
2:30remaining
Why does this AFTER DELETE trigger not delete related rows?

Given the following trigger, why does it fail to delete related rows in OrderDetails after deleting from Orders?

SQL
CREATE TRIGGER trg_AfterDelete
AFTER DELETE ON Orders
FOR EACH ROW
BEGIN
  DELETE FROM OrderDetails WHERE OrderID = OLD.OrderID;
END;

DELETE FROM Orders WHERE OrderID = 10;
AThe trigger should be BEFORE DELETE to access OLD values
BThe trigger syntax is correct and will delete related rows
CThe DELETE statement inside the trigger causes a recursion error
DThe trigger uses OLD but AFTER triggers cannot access OLD values
Attempts:
2 left
💡 Hint

Consider if AFTER DELETE triggers can access OLD values and perform deletes.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this AFTER UPDATE trigger

Which option correctly fixes the syntax error in this AFTER UPDATE trigger?

SQL
CREATE TRIGGER trg_AfterUpdate
AFTER UPDATE ON Employees
FOR EACH ROW
BEGIN
  IF NEW.Salary > OLD.Salary THEN
    UPDATE Employees SET Status = 'Promoted' WHERE EmployeeID = NEW.EmployeeID;
  END IF;
END;
AChange AFTER UPDATE to BEFORE UPDATE
BRemove END IF; because it's not needed in triggers
CReplace BEGIN with BEGIN TRANSACTION
DAdd THEN after IF condition: IF NEW.Salary > OLD.Salary THEN
Attempts:
2 left
💡 Hint

Check the IF statement syntax in SQL triggers.

optimization
expert
3:00remaining
Optimizing AFTER INSERT trigger to avoid unnecessary updates

An AFTER INSERT trigger updates a column Status to 'Active' for every new row. How can you optimize the trigger to avoid updating rows that already have Status = 'Active'?

ARemove the trigger and update Status manually after inserts
BChange the trigger to BEFORE INSERT and set Status directly
CAdd a WHERE clause to update only rows where Status is not 'Active'
DUse a DELETE statement inside the trigger to remove rows with Status 'Active'
Attempts:
2 left
💡 Hint

Think about how to limit updates to only necessary rows.