Merge Overlapping Events in the Same Hall - Problem

Given a table HallEvents that contains information about events held in different halls, write a SQL query to merge all overlapping events that occur in the same hall.

Two events are considered overlapping if they have at least one day in common. Events that overlap should be merged into a single event with the earliest start date and latest end date.

Return the result table showing the merged events for each hall in any order.

Table Schema

HallEvents
Column Name Type Description
hall_id int ID of the hall where the event is held
start_day date Start date of the event
end_day date End date of the event
Note: This table may contain duplicate rows. Each row represents an event in a specific hall with its duration.

Input & Output

Example 1 — Basic Overlapping Events
Input Table:
hall_id start_day end_day
1 2023-01-01 2023-01-03
1 2023-01-02 2023-01-04
1 2023-01-06 2023-01-07
Output:
hall_id start_day end_day
1 2023-01-01 2023-01-04
1 2023-01-06 2023-01-07
💡 Note:

The first two events in hall 1 overlap (Jan 1-3 and Jan 2-4 share Jan 2-3), so they merge into one event from Jan 1-4. The third event (Jan 6-7) doesn't overlap with the merged event, so it remains separate.

Example 2 — Multiple Halls
Input Table:
hall_id start_day end_day
1 2023-01-01 2023-01-02
2 2023-01-01 2023-01-03
2 2023-01-02 2023-01-04
Output:
hall_id start_day end_day
1 2023-01-01 2023-01-02
2 2023-01-01 2023-01-04
💡 Note:

Hall 1 has only one event, so no merging needed. Hall 2 has two overlapping events (Jan 1-3 and Jan 2-4) that merge into one event spanning Jan 1-4.

Example 3 — Adjacent Events (No Overlap)
Input Table:
hall_id start_day end_day
1 2023-01-01 2023-01-02
1 2023-01-03 2023-01-04
Output:
hall_id start_day end_day
1 2023-01-01 2023-01-02
1 2023-01-03 2023-01-04
💡 Note:

The events are adjacent (end of first event is Jan 2, start of second is Jan 3) but don't overlap since they don't share any common days. Both events remain separate.

Constraints

  • 1 ≤ hall_id ≤ 1000
  • start_day ≤ end_day
  • Table may contain duplicate rows
  • Dates are in YYYY-MM-DD format

Visualization

Tap to expand
Merge Overlapping Events ProblemTimeline visualization of event merging in Hall 1Before MergingEvent 1: Jan 1-3Event 2: Jan 2-4Event 3: Jan 6-7Jan 1Jan 3Jan 4Jan 6Jan 7MERGEAfter MergingMerged: Jan 1-4Separate: Jan 6-7Jan 1Jan 4Jan 6Jan 7Key InsightEvents 1 and 2 overlap on Jan 2-3, so they merge into one eventEvent 3 has no overlap with the merged event, so it remains separateSQL Strategy1. Use LAG() to compare current start with previous end2. Assign group numbers to overlapping events
Understanding the Visualization
1
Input
Events with potential overlaps
2
Window Function
Detect and group overlaps
3
Output
Merged non-overlapping events
Key Takeaway
🎯 Key Insight: Use window functions to identify overlapping intervals and merge them efficiently
Asked in
Amazon 28 Microsoft 22 Google 18
35.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen