Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does date range overlap detection in SQL help you find?
SELECT * FROM bookings WHERE start_date <= '2024-05-10' AND end_date >= '2024-05-05';
easy
A. If dates are in the future only
B. If two date ranges share any common days
C. If dates are stored as strings
D. If one date range is completely before another

Solution

  1. Step 1: Understand the condition

    The query checks if the booking's start date is on or before May 10 and the end date is on or after May 5.
  2. Step 2: Interpret the overlap logic

    This means the booking period overlaps with the date range May 5 to May 10.
  3. Final Answer:

    If two date ranges share any common days -> Option B
  4. Quick Check:

    Date overlap means shared days = D [OK]
Hint: Check if start <= other end and end >= other start [OK]
Common Mistakes:
  • Thinking overlap means one range is inside another
  • Confusing date comparison operators
  • Ignoring equal dates as overlap
2. Which SQL WHERE clause correctly detects overlap between two date ranges (start1, end1) and (start2, end2)?
easy
A. WHERE start1 < end2 AND end1 > start2
B. WHERE start1 > end2 OR end1 < start2
C. WHERE start1 = end2 AND end1 = start2
D. WHERE start1 <= end2 AND end1 >= start2

Solution

  1. Step 1: Recall overlap condition

    Two ranges overlap if the start of one is on or before the end of the other, and the end of one is on or after the start of the other.
  2. Step 2: Match condition to options

    WHERE start1 <= end2 AND end1 >= start2 uses <= and >= which correctly includes touching dates as overlap.
  3. Final Answer:

    WHERE start1 <= end2 AND end1 >= start2 -> Option D
  4. Quick Check:

    Overlap needs <= and >=, not just < or > [OK]
Hint: Use <= and >= to include touching dates as overlap [OK]
Common Mistakes:
  • Using < and > excludes touching dates
  • Using OR instead of AND
  • Checking equality only
3. Given the table events(event_id, start_date, end_date) with data:
1, '2024-05-01', '2024-05-05'
2, '2024-05-04', '2024-05-10'
3, '2024-05-11', '2024-05-15'

What rows will this query return?
SELECT * FROM events WHERE start_date <= '2024-05-06' AND end_date >= '2024-05-04';
medium
A. Rows with event_id 1 and 2
B. Rows with event_id 2 and 3
C. Only row with event_id 2
D. All rows

Solution

  1. Step 1: Check each event against the condition

    Condition: start_date <= '2024-05-06' AND end_date >= '2024-05-04'.
    Event 1: start '2024-05-01' <= '2024-05-06' (true), end '2024-05-05' >= '2024-05-04' (true) -> included.
    Event 2: start '2024-05-04' <= '2024-05-06' (true), end '2024-05-10' >= '2024-05-04' (true) -> included.
    Event 3: start '2024-05-11' <= '2024-05-06' (false) -> excluded.
  2. Step 2: Determine final result set

    Only events 1 and 2 satisfy the overlap condition.
  3. Final Answer:

    Rows with event_id 1 and 2 -> Option A
  4. Quick Check:

    Events overlapping May 4-6 are 1 and 2 [OK]
Hint: Test each row's start and end against the range [OK]
Common Mistakes:
  • Including event 3 which starts after the range
  • Ignoring the >= condition on end_date
  • Assuming only exact matches count
4. Identify the error in this SQL query intended to find overlapping bookings:
SELECT * FROM bookings WHERE start_date > '2024-05-01' AND end_date < '2024-05-10';
medium
A. All of the above
B. It uses > and < instead of >= and <=, missing overlaps
C. It only finds bookings fully inside the range, not overlapping ones
D. It excludes bookings that start or end exactly on the boundary dates

Solution

  1. Step 1: Analyze the condition logic

    The query uses start_date > '2024-05-01' and end_date < '2024-05-10', which means it only selects bookings fully inside the range, excluding those starting or ending exactly on the boundaries.
  2. Step 2: Understand overlap detection requirements

    Overlap detection requires using <= and >= to include touching dates and also to detect partial overlaps, not just fully contained bookings.
  3. Step 3: Combine observations

    All options B, C, and D are true, so the query is incorrect for overlap detection.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    All B, C, D are errors = A [OK]
Hint: Use inclusive operators and allow partial overlaps [OK]
Common Mistakes:
  • Using strict > and < excludes boundary overlaps
  • Assuming fully inside means overlap
  • Not testing edge cases
5. You have a table reservations(id, start_date, end_date). Write a query to find all reservations that overlap with a new booking from '2024-06-10' to '2024-06-15'. Which query correctly detects overlaps including edge cases?
hard
A. SELECT * FROM reservations WHERE start_date < '2024-06-15' AND end_date > '2024-06-10';
B. SELECT * FROM reservations WHERE start_date BETWEEN '2024-06-10' AND '2024-06-15';
C. SELECT * FROM reservations WHERE start_date <= '2024-06-15' AND end_date >= '2024-06-10';
D. SELECT * FROM reservations WHERE end_date BETWEEN '2024-06-10' AND '2024-06-15';

Solution

  1. Step 1: Understand overlap with inclusive edges

    Overlap means any shared day, including when one reservation ends exactly when the new booking starts or vice versa.
  2. Step 2: Evaluate each option

    SELECT * FROM reservations WHERE start_date < '2024-06-15' AND end_date > '2024-06-10'; uses < and > which excludes exact boundary matches.
    SELECT * FROM reservations WHERE start_date <= '2024-06-15' AND end_date >= '2024-06-10'; uses <= and >= which includes edge overlaps.
    Options C and D only check if start or end is inside the new booking range, missing some overlaps.
  3. Step 3: Choose the correct query

    SELECT * FROM reservations WHERE start_date <= '2024-06-15' AND end_date >= '2024-06-10'; correctly detects all overlaps including edge cases.
  4. Final Answer:

    SELECT * FROM reservations WHERE start_date <= '2024-06-15' AND end_date >= '2024-06-10'; -> Option C
  5. Quick Check:

    Inclusive operators catch all overlaps = B [OK]
Hint: Use <= and >= to catch all overlaps including edges [OK]
Common Mistakes:
  • Using < and > excludes edge overlaps
  • Using BETWEEN misses partial overlaps
  • Checking only start or end date
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard