On this page
  1. DML & Querying
  2. Contents
  3. INSERT, UPDATE, DELETE & RETURNING
  4. Upserts (ON CONFLICT DO UPDATE)
  5. Three-Valued NULL Logic

DML & Querying

Prev: DDL & Constraints | Up: sql | Next: Joins & Set Operations
Rel: SQL Query Execution Lifecycle, Python sqlite3 Patterns
Ref: PostgreSQL — Data Manipulation
Tags: #public #databases #sql #dml


Contents


INSERT, UPDATE, DELETE & RETURNING

Modern SQLite (3.35+) and PostgreSQL support RETURNING to fetch generated IDs or modified rows in a single round-trip:

INSERT INTO users (email, role)
VALUES ('ada@example.com', 'admin')
RETURNING id, email, created_at;

UPDATE users
SET role = 'member'
WHERE email = 'ada@example.com'
RETURNING id, role;

Upserts (ON CONFLICT DO UPDATE)

Atomically insert or update on a unique/primary key conflict without race conditions:

INSERT INTO daily_metrics (day, hits)
VALUES ('2026-11-23', 1)
ON CONFLICT (day)
DO UPDATE SET hits = daily_metrics.hits + excluded.hits;

Three-Valued NULL Logic

NULL means unknown, not zero or empty string. Comparisons with = or != against NULL evaluate to UNKNOWN (falsy in WHERE):

Linked from

Related notes