Basic Transactions: Functions
Module: SQL Fundamentals
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
BEGIN;
UPDATE employees SET salary = salary * 1.1;
-- Oops, wrong! Undo changes
ROLLBACK;
BEGIN;
DELETE FROM old_records WHERE created_at < '2020-01-01';
-- Check affected rows
SELECT COUNT(*) FROM old_records;
-- If correct: COMMIT, if wrong: ROLLBACK
COMMIT;
BEGIN;
INSERT INTO orders (customer_id, total) VALUES (123, 100);
INSERT INTO order_items (order_id, product_id) VALUES (LAST_INSERT_ID(), 456);
-- If any error occurs, ROLLBACK
COMMIT;
BEGIN: Start transaction
COMMIT: Save changes permanently
ROLLBACK: Undo all changes since BEGIN
Transactions are atomic (all or nothing)
Auto-commit: Each statement is a transaction by default
Explicit transactions override auto-commit
Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.
=
Returns rows where the left and right values are exactly equal.
column = value
Use with exact matches. Do not use = NULL.
<, >, <=, >=
Range comparison operators for less-than, greater-than, and inclusive boundary checks.
salary >= 80000
ANY / ALL
Compares one value against every or at least one value from a subquery result.
salary > ALL (SELECT salary FROM interns)