24.10. Coverings

Mikito Harakiri proposed the problem of writing the shortest SQL query that would return a minimal cover of a set of intervals. For example, given this table, how do you find the contiguous numbers that are completely covered by the given intervals?

CREATE TABLE Intervals (x INTEGER NOT NULL, y INTEGER NOT NULL, CHECK (x <= y), PRIMARY KEY (x, y)); INSERT INTO Intervals VALUES (1, 3); INSERT INTO Intervals VALUES (2, 5); INSERT INTO Intervals VALUES (4, 11); INSERT INTO Intervals VALUES (10, 12); INSERT INTO Intervals VALUES (20, 21); INSERT INTO Intervals VALUES (120, 130); INSERT INTO Intervals VALUES (120, 128); INSERT INTO Intervals VALUES (120, 122); INSERT INTO Intervals VALUES (121, 132); INSERT INTO Intervals VALUES ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.