← Back to SQL, page 10

sql-0186 · Hard senior

Can a CHECK constraint enforce a rule that involves other rows, such as 'a department may have at most 10 employees'?

No. CHECK constraints see only the current row, and subqueries are not allowed in them on SQLite or PostgreSQL. Cross-row rules need a trigger, a constraint backed by an index (unique, exclusion), or application logic with proper locking. Triggers must handle concurrency, or two inserts can both pass the check.

Full answer

A subquery in a CHECK is rejected:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
CREATE TABLE t (dept_id INTEGER CHECK ((SELECT COUNT(*) FROM employees) < 10));  -- error on purpose
text
Error: subqueries prohibited in CHECK constraints
sql
CREATE TABLE t (dept_id INTEGER CHECK ((SELECT COUNT(*) FROM employees) < 10));  -- error on purpose
text
Error: cannot use subquery in check constraint

A trigger can enforce the limit. The sample uses a limit of 3 so that Engineering, which already has three people, is full:

sql
CREATE TRIGGER dept_limit BEFORE INSERT ON employees
WHEN (SELECT COUNT(*) FROM employees WHERE dept_id = NEW.dept_id) >= 3
BEGIN
  SELECT RAISE(ABORT, 'department is full');
END;
INSERT INTO employees (id, name, dept_id) VALUES (9, 'Nia', 1);
-- error on purpose
text
Error: department is full

Under concurrency the trigger is not enough on its own in PostgreSQL. Two transactions can each count 9, insert, and commit 11. Fixes include locking the parent department row (SELECT ... FOR UPDATE) before inserting, using SERIALIZABLE isolation, or keeping a counter column on the department with a CHECK on it. SQLite allows only one writer at a time, which avoids this specific race.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

-- error on purpose
CREATE TABLE t (dept_id INTEGER CHECK ((SELECT COUNT(*) FROM employees) < 10));

-- error on purpose
CREATE TRIGGER dept_limit BEFORE INSERT ON employees
WHEN (SELECT COUNT(*) FROM employees WHERE dept_id = NEW.dept_id) >= 3
BEGIN
  SELECT RAISE(ABORT, 'department is full');
END;
INSERT INTO employees (id, name, dept_id) VALUES (9, 'Nia', 1);

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • CHECK constraints are row-local
  • Subqueries in CHECK are rejected
  • Triggers can enforce cross-row rules
  • Concurrency needs locking or serializable isolation

Common mistakes

  • Trusting a count-then-insert trigger under concurrent writes
  • Trying to put subqueries in CHECK

Follow-ups

  • How would a counter column with a CHECK work?
  • What is an exclusion constraint?

Tips

  • Raise the concurrency issue unprompted