0
0
SQLquery~20 mins

INSERT trigger in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
INSERT 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 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;
A(3, NULL)
B(NULL, NULL)
C(3, 'Alice')
DNo rows (empty table)
Attempts:
2 left
💡 Hint
Think about what the trigger does after a new employee is inserted.
🧠 Conceptual
intermediate
1:30remaining
Which statement about INSERT triggers is true?
Consider these statements about INSERT triggers in SQL. Which one is correct?
AAn INSERT trigger automatically fires on UPDATE statements as well.
BAn AFTER INSERT trigger can modify the row being inserted before it is saved.
CAn INSERT trigger can only be defined on tables with a primary key.
DA BEFORE INSERT trigger can prevent the insertion by raising an error.
Attempts:
2 left
💡 Hint
Think about when BEFORE and AFTER triggers run and what they can do.
📝 Syntax
advanced
2: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;
AAdd a semicolon after SET NEW.name = UPPER(NEW.name);
BReplace SET with SELECT
CAdd a semicolon after END;
DAdd a semicolon after BEGIN;
Attempts:
2 left
💡 Hint
Check statement termination inside the trigger body.
optimization
advanced
2: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;
AAdd IF NEW.department = 'Engineering' THEN before the INSERT statement and END IF after it.
BAdd WHERE department = 'Engineering' in the INSERT statement.
CChange AFTER INSERT to BEFORE INSERT and add a condition.
DAdd a CHECK constraint on hire_log for department = 'Engineering'.
Attempts:
2 left
💡 Hint
Use conditional logic inside the trigger body.
🔧 Debug
expert
3: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;
AThe trigger syntax is invalid because AFTER INSERT triggers cannot insert rows.
BThe trigger causes infinite recursion by inserting into the same table it listens to.
CThe trigger causes a deadlock because it locks the employees table twice.
DThe trigger fails because NEW.id + 1000 is not a valid expression.
Attempts:
2 left
💡 Hint
Think about what happens when the trigger inserts into the same table it is triggered on.