Challenge - 5 Problems
INSERT Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output after inserting a new row?
Given the table employees and the following INSERT trigger that logs new hires into hire_log, what rows will
hire_log contain after inserting (id=3, name='Alice', department='Sales') into employees?SQL
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50)); CREATE TABLE hire_log (emp_id INT, emp_name VARCHAR(50)); CREATE TRIGGER trg_after_insert AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO hire_log (emp_id, emp_name) VALUES (NEW.id, NEW.name); END; INSERT INTO employees VALUES (3, 'Alice', 'Sales'); SELECT * FROM hire_log;
Attempts:
2 left
💡 Hint
Think about what the trigger does after a new employee is inserted.
✗ Incorrect
The AFTER INSERT trigger copies the new employee's id and name into the hire_log table. So after inserting Alice, hire_log will have one row with (3, 'Alice').
🧠 Conceptual
intermediate1:30remaining
Which statement about INSERT triggers is true?
Consider these statements about INSERT triggers in SQL. Which one is correct?
Attempts:
2 left
💡 Hint
Think about when BEFORE and AFTER triggers run and what they can do.
✗ Incorrect
BEFORE INSERT triggers run before the row is inserted and can raise errors to prevent insertion. AFTER INSERT triggers run after insertion and cannot modify the inserted row. Triggers are not limited to tables with primary keys and do not fire on UPDATE unless defined for UPDATE.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this INSERT trigger
Which option correctly fixes the syntax error in this trigger code?
SQL
CREATE TRIGGER trg_insert BEFORE INSERT ON employees FOR EACH ROW BEGIN SET NEW.name = UPPER(NEW.name); END;
Attempts:
2 left
💡 Hint
Check statement termination inside the trigger body.
✗ Incorrect
In SQL triggers, each statement inside BEGIN...END must end with a semicolon. Missing it after SET causes syntax error.
❓ optimization
advanced2:30remaining
Optimize this INSERT trigger to avoid redundant logging
This trigger logs every inserted employee into hire_log. How can you optimize it to log only employees from the 'Engineering' department?
SQL
CREATE TRIGGER trg_after_insert AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO hire_log (emp_id, emp_name) VALUES (NEW.id, NEW.name); END;
Attempts:
2 left
💡 Hint
Use conditional logic inside the trigger body.
✗ Incorrect
You can use an IF statement inside the trigger to check the department before inserting into hire_log. WHERE clause is invalid in INSERT VALUES. BEFORE INSERT timing change is unrelated. CHECK constraints do not filter inserts.
🔧 Debug
expert3:00remaining
Why does this INSERT trigger cause a recursion error?
This trigger is defined on the employees table and inserts into employees inside the trigger body. What is the cause of the recursion error?
SQL
CREATE TRIGGER trg_recursive AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO employees (id, name, department) VALUES (NEW.id + 1000, NEW.name, NEW.department); END;
Attempts:
2 left
💡 Hint
Think about what happens when the trigger inserts into the same table it is triggered on.
✗ Incorrect
The trigger fires after an insert on employees, then inserts another row into employees, which fires the trigger again, causing infinite recursion.