Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 31/ 160
02. Advanced SQL & Relational Databases

Stored Procedures, User-Defined Functions (UDFs) & Triggers

Push logic into the database carefully with procedures, UDFs, and triggers - knowing the ops trade-offs.

IntermediateProcedures · UDFs · Triggers3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Pure expression, transaction, or invariant. Next, avoid row loops and network calls. Finally, migration, timings, and side-effect counts.

Think of it like…

UDFs are calculation tools, procedures are controlled maintenance scripts, and triggers are automatic interlocks: powerful inside the engine, dangerous when they secretly call the outside world.

Where you’ve seen it

A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.

Words you’ll hear

Procedures
Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.
UDFs
User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.
Triggers
Callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction.
Operational boundary
Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic. Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

Visual diagram

Loading interactive diagram

Theory & deep dive

Procedures: the working mechanism

Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction.

Pure expression, transaction, or invariant. Avoid row loops and network calls, then know commit and exception boundaries; this exposes the state governed by Triggers.

Stored Procedures, User-Defined Functions & Triggers in one concrete design

A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.

Key takeaway: Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction

Stored Procedures, User-Defined Functions & Triggers: failure mode and production response

Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.

Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution. Track Rows per call as "prefer set-based processing to row-by-row PL loops", and make Volatility visible to the dataset owner.

Key takeaway: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

How it works, step by step

Step 1 of 4
Procedures

1. Choose scope

Pure expression, transaction, or invariant. Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction.

pure expression, transaction, or invariant
Key numbers at this step
Rows per call
prefer set-based processing to row-by-row PL loops
UDFs
User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.

When to use it & trade-offs

What it is

Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction. A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.

Why it matters: Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic. Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

When to reach for it

  • Push logic into the database carefully with procedures, UDFs, and triggers - knowing the ops trade-offs.
  • Choose Procedures because it provides this capability: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.
  • Keep evidence for Rows per call at "prefer set-based processing to row-by-row PL loops" so the last production stage-Version and observe - migration, timings, and side-effect counts-remains testable.

When to avoid or be careful

  • Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
  • Do not conflate UDFs with Procedures. The second concept contributes different behavior: User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.
  • Release only after this guardrail is in place: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

Procedures / UDFs / Triggers - roles, limits, and operating cost

Procedures
Best for: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.

Strengths: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support. Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction.

Watch out: Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.

UDFs
Best for: User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.

Strengths: User functions with scalar or set inputs and declared volatility that can participate in queries and indexes. In this workflow it supports: Control transaction - know commit and exception boundaries.

Watch out: The operating cost is real: declare IMMUTABLE/STABLE/VOLATILE correctly.

Triggers
Best for: Callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction.

Strengths: Callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction. Its output can be checked with Trigger side effects.

Watch out: Operating guardrail: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Procedures, and how would Rows per call reveal it?”
  • “Why is UDFs distinct from this behavior: callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction?”
  • Production clue: Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
  • Design probe: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

Benchmarks & rules of thumb

Rows per call -
prefer set-based processing to row-by-row PL loops
Volatility -
declare IMMUTABLE/STABLE/VOLATILE correctly
Trigger side effects -
one source event should not create unbounded downstream work

Common mistakes & follow-ups

Common interview mistakes

  • Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
  • Reporting success from a job exit code without checking Rows per call and Volatility.
  • Treating this example as complete without replay, rollback, or access boundaries: A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.
  • What does the recovery plan do when this occurs? Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
  • When is Procedures insufficient for Volatility, and what does adding UDFs cost in Rows per call?
Interview tip: Lead with the data path and failure boundary: Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction. Then make the answer operational: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Stored Procedures, User-Defined Functions (UDFs) & Triggers with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Views, Materialized Views & Refresh Strategies
Next topic
B-Tree, Hash, GIN, GiST & BRIN Indexes