sqlcmd.net validated sql reference
advanced transactions MySQL MariaDB SQL Server PostgreSQL SQLite

Set Transaction Isolation Level

Control how much a transaction can see concurrent changes made by other transactions.

Docker-validated Not currently validation-green

Run a read transaction at SERIALIZABLE isolation

The transaction reads the account total under a stronger consistency mode. This matters when other sessions may be changing the same logical dataset concurrently.

MySQL MariaDB
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, balance INT);

INSERT INTO
  accounts
VALUES
  (1, 100),
  (2, 200);
SQL
SET
  TRANSACTION ISOLATION LEVEL SERIALIZABLE;

START TRANSACTION;

SELECT
  SUM(balance) AS total_balance
FROM
  accounts;

COMMIT;
total_balance
300
SQL Server
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, balance INT);

INSERT INTO
  accounts
VALUES
  (1, 100),
  (2, 200);
SQL
SET
  TRANSACTION ISOLATION LEVEL SERIALIZABLE;

BEGIN TRANSACTION;

SELECT
  SUM(balance) AS total_balance
FROM
  accounts;

COMMIT;
total_balance
300
PostgreSQL
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, balance INT);

INSERT INTO
  accounts
VALUES
  (1, 100),
  (2, 200);
SQL
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;

SELECT
  SUM(balance) AS total_balance
FROM
  accounts;

COMMIT;
total_balance
300
SQLite
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, balance INT);

INSERT INTO
  accounts
VALUES
  (1, 100),
  (2, 200);
SQL
BEGIN IMMEDIATE;

SELECT
  SUM(balance) AS total_balance
FROM
  accounts;

COMMIT;
total_balance
300

SQLite uses BEGIN IMMEDIATE to take a write-reserved lock rather than the standard isolation-level syntax.

Where this command helps.

  • running a critical read-modify-write sequence
  • preventing inconsistent reads during a multi-statement transaction

What the command is doing.

Transaction isolation determines which concurrency anomalies a transaction can observe, such as dirty reads, non-repeatable reads, and phantom rows. Common levels include READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. The exact defaults and behavior vary by engine, but the command is useful before a transaction that needs stronger consistency than the database default.