Bird
Raised Fist0
SQLquery~10 mins

AFTER trigger execution in SQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an AFTER INSERT trigger on the employees table.

SQL
CREATE TRIGGER trg_after_insert ON employees
AFTER [1]
AS
BEGIN
  PRINT 'Employee inserted';
END;
Drag options to blanks, or click blank then click option'
ADELETE
BUPDATE
CINSERT
DSELECT
Attempts:
3 left
💡 Hint
Common Mistakes
Using UPDATE or DELETE instead of INSERT for an insert trigger.
Trying to use SELECT which is not a trigger event.
2fill in blank
medium

Complete the code to reference the inserted row in an AFTER INSERT trigger.

SQL
CREATE TRIGGER trg_after_insert_salary ON employees
AFTER INSERT
AS
BEGIN
  SELECT salary FROM [1];
END;
Drag options to blanks, or click blank then click option'
Adeleted
Binserted
Cemployees
Dnew_rows
Attempts:
3 left
💡 Hint
Common Mistakes
Using deleted instead of inserted.
Using the base table name instead of the inserted pseudo-table.
3fill in blank
hard

Fix the error in the AFTER DELETE trigger code to correctly reference the deleted rows.

SQL
CREATE TRIGGER trg_after_delete ON employees
AFTER DELETE
AS
BEGIN
  SELECT * FROM [1];
END;
Drag options to blanks, or click blank then click option'
Adeleted
Bemployees
Cremoved
Dinserted
Attempts:
3 left
💡 Hint
Common Mistakes
Using inserted in a DELETE trigger.
Using the base table name instead of the deleted pseudo-table.
4fill in blank
hard

Fill both blanks to create an AFTER UPDATE trigger that logs old and new salaries.

SQL
CREATE TRIGGER trg_after_update_salary ON employees
AFTER UPDATE
AS
BEGIN
  INSERT INTO salary_log (old_salary, new_salary)
  SELECT [1].salary, [2].salary
  FROM deleted [1], inserted [2]
  WHERE [1].employee_id = [2].employee_id;
END;
Drag options to blanks, or click blank then click option'
Ad
Bi
Cold
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using full table names without aliases.
Mixing up inserted and deleted aliases.
5fill in blank
hard

Fill all three blanks to create an AFTER INSERT trigger that updates a summary table with the new employee count.

SQL
CREATE TRIGGER trg_after_insert_count ON employees
AFTER INSERT
AS
BEGIN
  UPDATE employee_summary
  SET total_employees = total_employees + [1]
  WHERE department_id = (SELECT [2] FROM inserted LIMIT 1);
  PRINT '[3] employees added';
END;
Drag options to blanks, or click blank then click option'
A1
Bdepartment_id
DNew
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names.
Forgetting to update the correct department.

Practice

(1/5)
1. What is the main purpose of an AFTER trigger in SQL?
easy
A. To execute actions only after a successful data change like insert, update, or delete
B. To prevent data changes before they happen
C. To rollback transactions automatically
D. To create new tables after data insertion

Solution

  1. Step 1: Understand the timing of AFTER triggers

    AFTER triggers run only after the main data change operation completes successfully.
  2. Step 2: Identify the purpose of AFTER triggers

    They automate tasks like logging or updating related data after changes, ensuring consistency.
  3. Final Answer:

    To execute actions only after a successful data change like insert, update, or delete -> Option A
  4. Quick Check:

    AFTER trigger = post-change action [OK]
Hint: AFTER triggers run only after successful data changes [OK]
Common Mistakes:
  • Confusing BEFORE and AFTER triggers
  • Thinking AFTER triggers run before data changes
  • Assuming AFTER triggers rollback transactions
2. Which of the following is the correct syntax to create an AFTER INSERT trigger in SQL?
easy
A. CREATE TRIGGER trg ON table_name AFTER DELETE BEGIN ... END;
B. CREATE TRIGGER trg AFTER INSERT ON table_name BEGIN ... END;
C. CREATE TRIGGER trg BEFORE INSERT ON table_name BEGIN ... END;
D. CREATE AFTER TRIGGER trg ON table_name INSERT BEGIN ... END;

Solution

  1. Step 1: Recall standard AFTER trigger syntax

    The correct syntax starts with CREATE TRIGGER, followed by trigger name, timing (AFTER), event (INSERT), and table name.
  2. Step 2: Check each option

    CREATE TRIGGER trg AFTER INSERT ON table_name BEGIN ... END; matches the correct syntax. Options A, B, and C have wrong order, timing, or event.
  3. Final Answer:

    CREATE TRIGGER trg AFTER INSERT ON table_name BEGIN ... END; -> Option B
  4. Quick Check:

    CREATE TRIGGER + AFTER + INSERT = correct syntax [OK]
Hint: AFTER triggers syntax: CREATE TRIGGER name AFTER event ON table [OK]
Common Mistakes:
  • Mixing BEFORE and AFTER keywords
  • Placing AFTER in wrong position
  • Using DELETE event instead of INSERT
3. Given this trigger and table:
CREATE TABLE sales (id INT, amount INT);
CREATE TRIGGER trg AFTER INSERT ON sales
BEGIN
INSERT INTO audit_log VALUES ('insert', NEW.id, NEW.amount);
END;

What happens when you run INSERT INTO sales VALUES (1, 100);?
medium
A. New rows are added to both sales and audit_log
B. A new row is added to audit_log only
C. A new row is added to sales only
D. No rows are added due to trigger error

Solution

  1. Step 1: Understand the trigger action

    The AFTER INSERT trigger inserts a row into audit_log using the new sales row data.
  2. Step 2: Analyze the insert command effect

    Inserting into sales triggers the AFTER INSERT, so both sales and audit_log get new rows.
  3. Final Answer:

    New rows are added to both sales and audit_log -> Option A
  4. Quick Check:

    AFTER INSERT triggers run after insert, adding audit_log row [OK]
Hint: AFTER INSERT triggers run after insert, affecting related tables [OK]
Common Mistakes:
  • Thinking trigger replaces original insert
  • Assuming trigger runs before insert
  • Ignoring NEW keyword usage
4. You wrote this AFTER UPDATE trigger:
CREATE TRIGGER trg AFTER UPDATE ON employees
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE id = NEW.id;
END;

What problem will occur when you update an employee's salary?
medium
A. Salary will not change
B. Trigger will not run at all
C. Syntax error prevents trigger creation
D. Infinite loop causing stack overflow

Solution

  1. Step 1: Analyze trigger logic

    The trigger updates the same table it is triggered on, causing the trigger to fire again.
  2. Step 2: Identify the effect of recursive updates

    This causes an infinite loop of updates, eventually causing stack overflow or error.
  3. Final Answer:

    Infinite loop causing stack overflow -> Option D
  4. Quick Check:

    Trigger updating same table without stop = infinite loop [OK]
Hint: Avoid triggers updating same table without condition to stop recursion [OK]
Common Mistakes:
  • Not realizing trigger recursion
  • Expecting trigger to run once only
  • Ignoring NEW and OLD references
5. You want to log every DELETE from the orders table into orders_log only if the delete succeeds. Which is the best way to do this using an AFTER trigger?
hard
A. Create an AFTER INSERT trigger on orders_log that deletes from orders
B. Create a BEFORE DELETE trigger that inserts into orders_log before deletion
C. Create an AFTER DELETE trigger on orders that inserts deleted row info into orders_log
D. Create a trigger that deletes from orders_log after orders delete

Solution

  1. Step 1: Understand timing for logging deletes

    AFTER DELETE triggers run only after the delete succeeds, ensuring accurate logging.
  2. Step 2: Choose correct trigger type

    Creating an AFTER DELETE trigger on orders to insert into orders_log captures deleted data reliably.
  3. Final Answer:

    Create an AFTER DELETE trigger on orders that inserts deleted row info into orders_log -> Option C
  4. Quick Check:

    AFTER DELETE trigger logs only successful deletions [OK]
Hint: Use AFTER DELETE triggers to log only successful deletions [OK]
Common Mistakes:
  • Using BEFORE DELETE triggers which may log failed deletes
  • Confusing INSERT triggers with DELETE triggers
  • Trying to delete from log table after orders delete
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