Skip to the page
Tecyfy

SQL interview question · medium

You have a table of employees and their departments. You need to ensure that no employee can belong to more than one department at the same time. How would you enforce this constraint in the database?

The short answerMake the database enforce it with a constraint, not application checks.

The full answer

Make the database enforce it with a constraint, not application checks. If you store only the current department, a key on the employee does it. If you keep history, use a unique index on current assignments, or in PostgreSQL an exclusion constraint that forbids overlapping periods.

1. Only the current department The simplest model puts the department on the employee:

CREATE TABLE employees (
    employee_id   INT PRIMARY KEY,
    name          TEXT NOT NULL,
    department_id INT NOT NULL REFERENCES departments (department_id)
);

One column can hold only one department, so the rule is enforced by the design. With a separate assignment table, make employee_id its primary key (or UNIQUE): one row per employee.

A composite key such as PRIMARY KEY (employee_id, department_id) does not enforce the rule: it allows the same employee in many departments, and only stops the same pair appearing twice.

2. With history: one current assignment

CREATE TABLE employee_departments (
    employee_id   INT  NOT NULL REFERENCES employees (employee_id),
    department_id INT  NOT NULL REFERENCES departments (department_id),
    valid_from    DATE NOT NULL,
    valid_to      DATE,                      -- NULL = current
    CHECK (valid_to IS NULL OR valid_to > valid_from)
);

CREATE UNIQUE INDEX uq_one_current_department
    ON employee_departments (employee_id)
    WHERE valid_to IS NULL;

The partial (filtered) unique index allows any number of past assignments but only one open one. PostgreSQL, SQL Server and SQLite support it; in MySQL, a unique index on a generated column that is employee_id for current rows and NULL otherwise has the same effect.

3. With history: no overlapping periods (PostgreSQL) The partial index doesn't stop back-dated rows overlapping. An exclusion constraint does:

CREATE EXTENSION IF NOT EXISTS btree_gist;

ALTER TABLE employee_departments
    ADD CONSTRAINT no_overlapping_assignments
    EXCLUDE USING gist (
        employee_id WITH =,
        daterange(valid_from, valid_to) WITH &&
    );

Two rows for the same employee whose date ranges overlap are rejected; a NULL valid_to counts as open-ended.

Why not a trigger? A trigger that counts existing rows before inserting looks simple, but two concurrent transactions can each see no existing row and both insert. Constraints and unique indexes are enforced safely under concurrency; a trigger is only safe with extra locking.

Transfers Moving an employee becomes one transaction: close the current row (valid_to = today) and insert the new one. The constraints guarantee the result is valid however the application gets there. If the transaction fails half-way, both changes roll back, so the employee is never left with no department or two.

Last updated 16 October 2026

Sources

Where each fact comes from

  1. 1Constraints
  2. 2Partial indexes
  3. 3CREATE TABLE
  4. 4btree_gist