Find all pairs of rows with overlapping start and end times.

Given a table of data with three columns: id, start, and end, this SQL query will find all pairs of rows where the start and end times overlap.

Problem

Given a table of data with three columns: id, start, and end, write a SQL query to find all pairs of rows where the start and end times overlap.
Example input:
id | start | end
1 | 10 | 20
2 | 15 | 30
3 | 25 | 35
4 | 45 | 55
Example output:
1, 2
2, 3

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT t1.id, t2.id
FROM table t1
JOIN table t2
ON t1.start < t2.end AND t1.end > t2.start
WHERE t1.id < t2.id;

/*

The solution above is optimal because it uses a JOIN to find all pairs of rows that overlap.

*/

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a good understanding of SQL. The solution is also optimal, as it uses a JOIN to find all pairs of rows that overlap.

Evaluated at: 2022-11-20 12:16:49