Learn SQL
-- advanced.procedures_functions_triggers

Procedures, Functions & Triggers in SQL

Move invariants to the database β€” carefully

--concept

Functions (RETURN values, usable in SQL) vs procedures (CALL side-effects, txn control). Triggers fire per-row/statement (audit history, denormalized sync, INSTEAD OF on complex views). Power with costs: hidden logic, test burden, write amplification. Keep business rules visible; push integrity (audit, guards) that must survive every writer.

--how it works
1

Functions return values

Pure calculations callable from SELECT β€” testable, indexable where immutable.

2

Procedures run jobs

CALL side-effects with transaction control β€” backfills, maintenance.

3

Triggers enforce

AFTER for audit; INSTEAD OF maps complex-view writes. Survive every writer.

4

Version like code

Migrations + tests + rollback for all three. Hidden logic is tech debt.

--visualization
FUNCTIONPure values in SQL (IMMUTABLE indexable)
Stage 1 / 1
Version like code β€” migrations + tests + rollback.
--example
query.sql
1-- INSTEAD OF maps complex-view writes explicitly:
2-- CREATE TRIGGER v_write INSTEAD OF INSERT ON v ...
Result
tooljob
functionsvalues in SQL
proceduresjobs with txn control
triggersper-row enforcement

Pure calculations live in functions callable from SELECT. Multi-step maintenance lives in procedures. Cross-writer invariants (audit) live in triggers β€” the only layer bugs cannot bypass.

--variants

Pure bonus function

Deterministic math in SQL.

query.sql
1CREATE FUNCTION bonus_calc(s NUMERIC) RETURNS NUMERIC AS $$ SELECT s * 0.1 $$ LANGUAGE sql IMMUTABLE;
Result
callresult
bonus_calc(85000)8500
1Immutable flags enable index use.
2Unit-testable with fixed inputs.
3Volatile functions cannot index.

Complex-view write map

Explicit write routing where views cannot pass through.

query.sql
1-- CREATE TRIGGER v_write INSTEAD OF INSERT ON v FOR EACH ROW EXECUTE FUNCTION route_v();
Result
triggerjob
instead-ofwrites routed explicitly
1Simple views pass through (+ CHECK OPTION).
2Joined/aggregated views need explicit maps.
3Audit triggers ride AFTER for capture.
--key facts
Functionsreturn values, SQL-callable
ProceduresCALL side-effects + COMMIT
Triggerssurvive every writer β€” audit/guard
--use cases
Invariant enforcement that must survive consoles/jobs
Maintenance jobs with transactional steps
--under the hood
Row triggers cost per row + WAL β€” statement triggers batch better for bulk syncs.
Function volatility (IMMUTABLE/STABLE/VOLATILE) gates inlining and indexability.

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.