SQL Keywords

SQL ISOLATION

What is SQL ISOLATION?

Defines how and when the changes made by one transaction become visible to other concurrent transactions.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL ISOLATION: PostgreSQL, MySQL (InnoDB), SQL Server, Oracle, SQLite (partial), MariaDB, DB2, Snowflake

SQL ISOLATION Full Explanation

SQL ISOLATION refers to the isolation level applied to a transaction, controlling phenomena such as dirty reads, non-repeatable reads, and phantom reads. The SQL standard specifies four levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. Each higher level prevents more concurrency anomalies but can reduce throughput. Isolation is usually set by executing SET TRANSACTION ISOLATION LEVEL before a transaction begins, or by configuring a session or database default. Some databases add proprietary levels (e.g., PostgreSQL's REPEATABLE READ behaves like snapshot isolation, SQL Server offers SNAPSHOT, MySQL provides READ COMMITTED as default in InnoDB). Choosing the right level balances data correctness against performance. Lower levels allow higher concurrency but risk inconsistent data; higher levels guarantee consistency at the cost of locking or versioning overhead.

SQL ISOLATION Syntax

-- Session level
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL <level>;

-- Per-transaction (most common)
SET TRANSACTION ISOLATION LEVEL <level>;
BEGIN;
-- transactional work
COMMIT;

SQL ISOLATION Parameters

  • STRING One of READ UNCOMMITTED (READ COMMITTED) - REPEATABLE READ|||SERIALIZABLE (plus vendor-specific options such as SNAPSHOT)

Example Queries Using SQL ISOLATION

-- Example 1: PostgreSQL transaction
BEGIN;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

-- Example 2: SQL Server session default
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SNAPSHOT;
GO

-- Example 3: MySQL read-only analytics query
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION READ ONLY;
SELECT * FROM orders WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY);
COMMIT;

Expected Output Using SQL ISOLATION

  • The server records the requested isolation level in the session or transaction
  • Subsequent statements run with the specified guarantees, influencing locking strategy or MVCC behaviour
  • No result set is returned unless the enclosed statements produce one

Use Cases with SQL ISOLATION

  • Prevent dirty reads when financial accuracy is critical
  • Allow faster reporting queries with READ COMMITTED in analytics workloads
  • Enforce strict consistency in inventory management with SERIALIZABLE
  • Switch to SNAPSHOT in SQL Server to gain repeatable reads without heavy locking

Common Mistakes with SQL ISOLATION

  • Setting isolation after BEGIN; most databases require it before the first read/write
  • Assuming vendor levels behave identically (e.g., MySQL REPEATABLE READ vs PostgreSQL)
  • Forgetting to reset the level, leading to unexpected locking in later transactions
  • Believing READ COMMITTED prevents phantom reads (it does not)

Related Topics

SET TRANSACTION, COMMIT, ROLLBACK, SAVEPOINT, MVCC, LOCK TABLE

First Introduced In

SQL-92

Frequently Asked Questions

What is the default isolation level in PostgreSQL?

READ COMMITTED is the default. It prevents dirty reads but allows non-repeatable reads and phantom rows.

Can I mix isolation levels in one session?

Yes. You can set a different level for each transaction with SET TRANSACTION ISOLATION LEVEL without affecting subsequent transactions.

Is SERIALIZABLE always slower?

Not necessarily. MVCC implementations like PostgreSQL can achieve high concurrency under SERIALIZABLE, but write conflicts may trigger rollbacks if contention is heavy.

How do I check the current isolation level?

Query system views or variables. For example, in PostgreSQL: `SHOW default_transaction_isolation;` or `SHOW transaction_isolation;`

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!