Bird
0
0

Given a table bookings(start_date, end_date) with rows:

medium📝 query result Q4 of 15
SQL - Advanced Query Patterns
Given a table bookings(start_date, end_date) with rows:
1) 2024-06-01 to 2024-06-05
2) 2024-06-04 to 2024-06-10
What will this query return?
SELECT * FROM bookings b1 JOIN bookings b2 ON b1.start_date <= b2.end_date AND b1.end_date >= b2.start_date WHERE b1.start_date <> b2.start_date;
APairs of bookings that overlap but are not the same booking
BAll bookings including self-joins
COnly bookings that do not overlap
DNo rows because of the inequality filter
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the JOIN condition

    The JOIN matches bookings where their date ranges overlap using the overlap condition.
  2. Step 2: Consider the WHERE clause

    It excludes rows where start dates are equal, so it removes self-joins, leaving only overlapping pairs of different bookings.
  3. Final Answer:

    Pairs of bookings that overlap but are not the same booking -> Option A
  4. Quick Check:

    Overlap pairs excluding self = Pairs of bookings that overlap but are not the same booking [OK]
Quick Trick: JOIN on overlap condition and exclude self-joins [OK]
Common Mistakes:
  • Thinking it returns all bookings including self-joins
  • Assuming it returns non-overlapping bookings
  • Ignoring the WHERE clause filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes