Bird
0
0

Given the table events(event_id, start_date, end_date) with data:

medium📝 query result Q13 of 15
SQL - Advanced Query Patterns
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';
ARows with event_id 1 and 2
BRows with event_id 2 and 3
COnly row with event_id 2
DAll rows
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes