Challenge - 5 Problems
Date Range Overlap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Detect overlapping date ranges between two bookings
Given a table
bookings with columns id, start_date, and end_date, which SQL query correctly returns all pairs of bookings that overlap in their date ranges?SQL
CREATE TABLE bookings (id INT, start_date DATE, end_date DATE); INSERT INTO bookings VALUES (1, '2024-01-01', '2024-01-10'), (2, '2024-01-05', '2024-01-15'), (3, '2024-01-20', '2024-01-25');
Attempts:
2 left
💡 Hint
Think about when two date ranges overlap: they share at least one day in common.
✗ Incorrect
Two date ranges overlap if the start of one is on or before the end of the other, and its end is on or after the other's start. Option C correctly uses this logic and excludes self-joins by checking a.id <> b.id.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in date range overlap query
Which option contains a syntax error in the SQL query that tries to find overlapping date ranges between two tables
events and reservations?SQL
Tables: events(event_id INT, start_date DATE, end_date DATE) reservations(res_id INT, start_date DATE, end_date DATE)
Attempts:
2 left
💡 Hint
Check if the JOIN clause has a proper ON condition.
✗ Incorrect
Option A is missing the ON keyword and condition after JOIN, which is required syntax for JOIN operations in SQL.
❓ optimization
advanced2:30remaining
Optimize date range overlap query for large datasets
You have a large table
appointments with columns id, start_time, and end_time. Which query is the most efficient to find overlapping appointments without self-joining the entire table unnecessarily?SQL
CREATE INDEX idx_start_time ON appointments(start_time); CREATE INDEX idx_end_time ON appointments(end_time);
Attempts:
2 left
💡 Hint
Try to push conditions into the JOIN clause to reduce the number of rows joined.
✗ Incorrect
Option A places the overlap condition inside the JOIN clause, allowing the database engine to use indexes more effectively and reduce the join size compared to filtering after the join.
🔧 Debug
advanced2:00remaining
Why does this date range overlap query return duplicates?
The query below returns pairs of overlapping date ranges but includes duplicate pairs like (1,2) and (2,1). How can you fix it to avoid duplicates?
SQL
SELECT a.id AS id1, b.id AS id2 FROM bookings a JOIN bookings b ON a.start_date <= b.end_date AND a.end_date >= b.start_date WHERE a.id <> b.id;
Attempts:
2 left
💡 Hint
Think about how to order pairs to avoid reversed duplicates.
✗ Incorrect
Adding a.id < b.id ensures each pair is returned only once in a consistent order, preventing reversed duplicates like (2,1) when (1,2) is already included.
🧠 Conceptual
expert1:30remaining
Understanding date range overlap logic
Which of the following statements correctly describes the condition to detect if two date ranges
[start1, end1] and [start2, end2] overlap?Attempts:
2 left
💡 Hint
Overlap means the ranges share at least one common point in time.
✗ Incorrect
Option D correctly states the condition for two ranges to overlap: the start of one is before or on the end of the other, and its end is after or on the start of the other.