OVERLAPS Predicate Time Intervals

The SQL-standard OVERLAPS predicate compares two (start, end) interval pairs with half-open semantics, no range types required.

SqlData Integrity

Code

snippet
SELECT (TIME '09:00', TIME '10:30') OVERLAPS (TIME '10:00', TIME '11:00') AS truly_overlaps,
       (TIME '09:00', TIME '10:00') OVERLAPS (TIME '10:00', TIME '11:00') AS touching_is_false,
       (TIME '09:00', TIME '10:30') OVERLAPS (TIME '09:00', TIME '10:30') AS identical_is_true;

-- 排班表:找出与目标班次重叠的轮班,谓词直接写进 WHERE
CREATE TABLE shifts (
    shift_id  int PRIMARY KEY,
    worker    text NOT NULL,
    starts_at time NOT NULL,
    ends_at   time NOT NULL
);

INSERT INTO shifts VALUES
    (1, 'ada', TIME '09:00', TIME '12:00'),
    (2, 'lin', TIME '11:30', TIME '14:00'),
    (3, 'kai', TIME '14:00', TIME '18:00');

SELECT shift_id, worker, starts_at, ends_at
FROM shifts
WHERE (starts_at, ends_at) OVERLAPS (TIME '10:00', TIME '13:00')
ORDER BY shift_id;

DROP TABLE shifts;

Inside the snippet

Several statements share this file — SELECT / INSERT / CREATE, with shifts among the objects involved.

Mechanically, the statements are built from: a WHERE clause narrowing which rows one statement affects; an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity.

Putting it to use

The shifts table is the working surface here.

Core lines

SELECT shift_id, worker, starts_at, ends_at
FROM shifts
WHERE (starts_at, ends_at) OVERLAPS (TIME '10:00', TIME '13:00')
ORDER BY shift_id;
SELECT (TIME '09:00', TIME '10:30') OVERLAPS (TIME '10:00', TIME '11:00') AS truly_overlaps,
(TIME '09:00', TIME '10:00') OVERLAPS (TIME '10:00', TIME '11:00') AS touching_is_false,
(TIME '09:00', TIME '10:30') OVERLAPS (TIME '09:00', TIME '10:30') AS identical_is_true;
INSERT INTO shifts VALUES
(1, 'ada', TIME '09:00', TIME '12:00'),
(2, 'lin', TIME '11:30', TIME '14:00'),
(3, 'kai', TIME '14:00', TIME '18:00');

Interactive Tools for This Topic

FAQ

Which relations are involved here?
`shifts` — referenced directly, no join needed.