Bird
Raised Fist0
SQLquery~20 mins

DELETE trigger in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DELETE Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of DELETE trigger after row deletion

Consider a table Employees with columns id and salary. A DELETE trigger is defined to insert the deleted row's id and salary into a log table DeletedEmployees. What will be the content of DeletedEmployees after deleting the employee with id = 3?

SQL
CREATE TABLE Employees (id INT PRIMARY KEY, salary INT);
CREATE TABLE DeletedEmployees (id INT, salary INT);

CREATE TRIGGER trg_after_delete
AFTER DELETE ON Employees
FOR EACH ROW
INSERT INTO DeletedEmployees (id, salary) VALUES (OLD.id, OLD.salary);

INSERT INTO Employees VALUES (1, 5000), (2, 6000), (3, 7000);

DELETE FROM Employees WHERE id = 3;
A
id | salary
3  | 7000
B
id | salary
1  | 5000
2  | 6000
C
id | salary
3  | NULL
DEmpty table
Attempts:
2 left
💡 Hint

Think about what data the trigger captures when a row is deleted.

🧠 Conceptual
intermediate
1:30remaining
Understanding OLD and NEW in DELETE triggers

In a DELETE trigger, which of the following statements about OLD and NEW references is correct?

ANeither <code>OLD</code> nor <code>NEW</code> are available in DELETE triggers.
B<code>OLD</code> contains the row data before deletion; <code>NEW</code> is not available.
CBoth <code>OLD</code> and <code>NEW</code> contain the row data before deletion.
D<code>NEW</code> contains the row data before deletion; <code>OLD</code> is not available.
Attempts:
2 left
💡 Hint

Think about what data exists after a row is deleted.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in DELETE trigger definition

Which option contains a syntax error in the DELETE trigger definition?

SQL
CREATE TRIGGER trg_del
AFTER DELETE ON Orders
FOR EACH ROW
BEGIN
  INSERT INTO DeletedOrders (order_id) VALUES (OLD.order_id);
END;
ACREATE TRIGGER trg_del AFTER DELETE ON Orders FOR EACH ROW BEGIN INSERT INTO DeletedOrders (order_id) VALUES (OLD.order_id); END
BCREATE TRIGGER trg_del AFTER DELETE ON Orders FOR EACH ROW INSERT INTO DeletedOrders (order_id) VALUES (OLD.order_id);
CCREATE TRIGGER trg_del AFTER DELETE ON Orders FOR EACH ROW BEGIN INSERT INTO DeletedOrders (order_id) VALUES (OLD.order_id); END;
DCREATE TRIGGER trg_del AFTER DELETE ON Orders FOR EACH ROW BEGIN INSERT INTO DeletedOrders (order_id) VALUES (NEW.order_id); END;
Attempts:
2 left
💡 Hint

Check the use of OLD and NEW in DELETE triggers.

❓ optimization
advanced
2:30remaining
Optimizing DELETE trigger for bulk deletes

You have a DELETE trigger that logs deleted rows one by one. Which approach improves performance when deleting many rows at once?

AUse a statement-level trigger to insert all deleted rows in a single batch.
BUse a FOR EACH ROW trigger to insert each deleted row individually.
CDisable the trigger during bulk deletes and manually log rows later.
DAdd a delay in the trigger to reduce server load.
Attempts:
2 left
💡 Hint

Think about how triggers handle multiple rows.

🔧 Debug
expert
3:00remaining
Why does this DELETE trigger cause an infinite loop?

Given the trigger below, deleting a row from Products causes an infinite loop. Why?

CREATE TRIGGER trg_del_product
AFTER DELETE ON Products
FOR EACH ROW
BEGIN
  DELETE FROM Products WHERE id = OLD.id;
END;
AThe trigger tries to delete from a different table causing a deadlock.
BThe trigger uses OLD.id which is not available in AFTER DELETE triggers.
CThe trigger deletes the same row again, causing recursive trigger calls.
DThe trigger syntax is invalid and causes repeated errors.
Attempts:
2 left
💡 Hint

Consider what happens when the trigger deletes rows inside itself.

Practice

(1/5)
1. What is the main purpose of a DELETE trigger in a database?
easy
A. To insert new rows into a table
B. To update rows before they are deleted
C. To automatically run code when rows are deleted from a table
D. To prevent any data from being deleted

Solution

  1. Step 1: Understand what triggers do

    Triggers run automatically in response to certain database events like insert, update, or delete.
  2. Step 2: Focus on DELETE triggers

    A DELETE trigger specifically runs when rows are deleted, allowing automatic actions during that event.
  3. Final Answer:

    To automatically run code when rows are deleted from a table -> Option C
  4. Quick Check:

    DELETE trigger = runs on row deletion [OK]
Hint: DELETE triggers run code automatically on row deletions [OK]
Common Mistakes:
  • Confusing DELETE triggers with INSERT triggers
  • Thinking DELETE triggers prevent deletion by default
  • Assuming DELETE triggers update rows instead
2. Which of the following is the correct syntax to create a trigger that runs before a row is deleted from a table named Employees?
easy
A. CREATE TRIGGER before_delete_employee DELETE ON Employees FOR EACH ROW BEGIN --code END;
B. CREATE TRIGGER before_delete_employee AFTER DELETE ON Employees FOR EACH ROW BEGIN --code END;
C. CREATE TRIGGER before_delete_employee ON Employees BEFORE DELETE FOR EACH ROW BEGIN --code END;
D. CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END;

Solution

  1. Step 1: Identify correct trigger timing syntax

    The syntax for a BEFORE DELETE trigger includes 'BEFORE DELETE ON TableName'.
  2. Step 2: Check full syntax correctness

    CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END; correctly uses 'CREATE TRIGGER name BEFORE DELETE ON Employees FOR EACH ROW BEGIN ... END;'. Others misuse order or keywords.
  3. Final Answer:

    CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END; -> Option D
  4. Quick Check:

    BEFORE DELETE ON Table = correct syntax [OK]
Hint: Use BEFORE DELETE ON table for pre-delete triggers [OK]
Common Mistakes:
  • Placing BEFORE DELETE after ON table
  • Omitting FOR EACH ROW clause
  • Using DELETE ON instead of BEFORE DELETE ON
3. Given the following trigger and table data, what will be the output after deleting the row with id = 2 from Orders?
CREATE TRIGGER log_delete AFTER DELETE ON Orders
FOR EACH ROW
BEGIN
  INSERT INTO DeletedOrdersLog(order_id, deleted_at) VALUES (OLD.id, CURRENT_TIMESTAMP);
END;

-- Initial Orders table:
-- id | product
-- 1  | Book
-- 2  | Pen
-- 3  | Notebook

-- DeletedOrdersLog is initially empty.
medium
A. A new row with order_id = 2 is inserted into DeletedOrdersLog
B. The row with id = 2 is restored in Orders
C. No changes happen to DeletedOrdersLog
D. An error occurs because OLD.id is invalid

Solution

  1. Step 1: Understand AFTER DELETE trigger behavior

    The trigger runs after a row is deleted from Orders, so OLD.id refers to the deleted row's id.
  2. Step 2: Analyze trigger action

    The trigger inserts a new row into DeletedOrdersLog with order_id = OLD.id (2) and current timestamp.
  3. Final Answer:

    A new row with order_id = 2 is inserted into DeletedOrdersLog -> Option A
  4. Quick Check:

    AFTER DELETE inserts log row with OLD.id [OK]
Hint: AFTER DELETE triggers can log deleted row info using OLD [OK]
Common Mistakes:
  • Thinking OLD.id is not accessible in DELETE triggers
  • Assuming trigger restores deleted rows
  • Believing no action happens after delete
4. Consider this trigger code snippet intended to prevent deletion of rows from Users table if the user is an admin:
CREATE TRIGGER prevent_admin_delete BEFORE DELETE ON Users
FOR EACH ROW
BEGIN
  IF OLD.role = 'admin' THEN
    SIGNAL SQLSTATE '45000' MESSAGE_TEXT = 'Cannot delete admin user';
  END IF;
END;
What is the main issue if this trigger does not prevent admin deletions?
medium
A. The SIGNAL statement syntax is incorrect
B. The condition should check NEW.role instead of OLD.role
C. The trigger does not specify FOR EACH ROW
D. The trigger should be AFTER DELETE, not BEFORE DELETE

Solution

  1. Step 1: Check trigger timing and row context

    BEFORE DELETE and FOR EACH ROW are correctly used to check OLD.role before deletion.
  2. Step 2: Verify SIGNAL syntax

    In standard SQL, SIGNAL syntax requires correct SQLSTATE and message format. If syntax is wrong, error won't raise and deletion proceeds.
  3. Final Answer:

    The SIGNAL statement syntax is incorrect -> Option A
  4. Quick Check:

    SIGNAL syntax error prevents trigger from blocking delete [OK]
Hint: Check SIGNAL syntax carefully to block deletes [OK]
Common Mistakes:
  • Using AFTER DELETE instead of BEFORE DELETE
  • Checking NEW instead of OLD in DELETE triggers
  • Omitting FOR EACH ROW clause
5. You want to create a DELETE trigger on a Products table that archives deleted rows into an ArchivedProducts table, but only if the product price is above 100. Which trigger definition correctly implements this logic?
hard
A. CREATE TRIGGER archive_expensive_product BEFORE DELETE ON Products FOR EACH ROW BEGIN IF NEW.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (NEW.id, NEW.price); END IF; END;
B. CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END;
C. CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF NEW.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END;
D. CREATE TRIGGER archive_expensive_product BEFORE DELETE ON Products FOR EACH ROW BEGIN IF NEW.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END;

Solution

  1. Step 1: Choose correct timing and row reference

    AFTER DELETE triggers run after deletion; OLD refers to deleted row data. BEFORE DELETE can also work but OLD is preferred after deletion.
  2. Step 2: Check condition and insert logic

    Condition must check OLD.price > 100. Insert must use OLD column values to archive deleted row data.
  3. Step 3: Validate options

    CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END; uses AFTER DELETE, OLD.price, and inserts OLD.id, OLD.price correctly. Others use NEW or wrong timing.
  4. Final Answer:

    CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END; -> Option B
  5. Quick Check:

    AFTER DELETE + OLD.price condition + insert OLD columns = correct [OK]
Hint: Use AFTER DELETE and OLD to archive deleted rows conditionally [OK]
Common Mistakes:
  • Using NEW instead of OLD in DELETE triggers
  • Placing condition on NEW.price which is invalid
  • Using BEFORE DELETE but referencing OLD incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard