0
0
SQLquery~20 mins

Date range overlap detection in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Range Overlap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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');
ASELECT a.id AS booking1, b.id AS booking2 FROM bookings a JOIN bookings b ON a.id <> b.id WHERE a.start_date > b.end_date OR a.end_date < b.start_date;
BSELECT a.id AS booking1, b.id AS booking2 FROM bookings a JOIN bookings b ON a.id = b.id WHERE a.start_date < b.end_date AND a.end_date > b.start_date;
CSELECT a.id AS booking1, b.id AS booking2 FROM bookings a JOIN bookings b ON a.id <> b.id WHERE a.start_date <= b.end_date AND a.end_date >= b.start_date;
DSELECT a.id AS booking1, b.id AS booking2 FROM bookings a JOIN bookings b ON a.id <> b.id WHERE a.start_date >= b.end_date AND a.end_date <= b.start_date;
Attempts:
2 left
💡 Hint
Think about when two date ranges overlap: they share at least one day in common.
📝 Syntax
intermediate
2: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)
ASELECT e.event_id, r.res_id FROM events e JOIN reservations r WHERE e.start_date <= r.end_date AND e.end_date >= r.start_date;
BSELECT e.event_id, r.res_id FROM events e JOIN reservations r ON e.start_date <= r.end_date AND e.end_date >= r.start_date WHERE e.event_id <> r.res_id;
CSELECT e.event_id, r.res_id FROM events e JOIN reservations r ON e.start_date <= r.end_date AND e.end_date >= r.start_date;
DSELECT e.event_id, r.res_id FROM events e JOIN reservations r ON e.start_date < r.end_date AND e.end_date > r.start_date;
Attempts:
2 left
💡 Hint
Check if the JOIN clause has a proper ON condition.
optimization
advanced
2: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);
ASELECT a.id, b.id FROM appointments a JOIN appointments b ON a.id <> b.id AND a.start_time <= b.end_time AND a.end_time >= b.start_time;
BWITH filtered AS (SELECT * FROM appointments WHERE start_time < '2024-12-31') SELECT a.id, b.id FROM filtered a JOIN filtered b ON a.id <> b.id WHERE a.start_time <= b.end_time AND a.end_time >= b.start_time;
CSELECT a.id, b.id FROM appointments a JOIN appointments b ON a.id <> b.id WHERE a.start_time <= b.end_time AND a.end_time >= b.start_time;
DSELECT a.id, b.id FROM appointments a JOIN appointments b ON a.id <> b.id WHERE a.start_time < b.end_time AND a.end_time > b.start_time AND a.start_time < b.start_time;
Attempts:
2 left
💡 Hint
Try to push conditions into the JOIN clause to reduce the number of rows joined.
🔧 Debug
advanced
2: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;
AUse UNION ALL instead of JOIN.
BAdd a condition to only select pairs where a.id < b.id in the WHERE clause.
CRemove the WHERE clause filtering a.id <> b.id.
DChange the JOIN to LEFT JOIN to avoid duplicates.
Attempts:
2 left
💡 Hint
Think about how to order pairs to avoid reversed duplicates.
🧠 Conceptual
expert
1: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?
AThey overlap if start1 = start2 OR end1 = end2.
BThey overlap if start1 > end2 OR end1 < start2.
CThey overlap if start1 < start2 AND end1 > end2.
DThey overlap if start1 <= end2 AND end1 >= start2.
Attempts:
2 left
💡 Hint
Overlap means the ranges share at least one common point in time.