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 in the Same Hall INPUT: HallEvents Table hall_id start end 1 2023-01-01 2023-01-05 1 2023-01-03 2023-01-08 1 2023-01-10 2023-01-12 2 2023-02-01 2023-02-05 2 2023-02-04 2023-02-10 Hall 1 Timeline: Jan 1-5 Jan 3-8 Jan 10-12 Events overlap on Jan 3-5 Jan 10-12 is separate ALGORITHM STEPS 1 Group by hall_id Partition events by hall 2 Sort by start_date Order events chronologically 3 Detect overlaps Use LAG to compare with previous end_date 4 Assign group IDs SUM running total for non-overlapping events 5 Merge groups MIN(start), MAX(end) GROUP BY hall_id, grp SELECT hall_id, MIN(start), MAX(end) FINAL RESULT hall_id start end 1 2023-01-01 2023-01-08 1 2023-01-10 2023-01-12 2 2023-02-01 2023-02-10 Hall 1 Merged: Jan 1-8 (merged) Jan 10-12 Hall 2 Merged: Feb 1-10 (merged) 5 rows --> 3 rows Overlapping events merged! Key Insight: Window Functions for Gap-and-Island Detection Use LAG() to compare each event's start_date with the previous event's end_date within the same hall. When start_date > prev_end_date, a new group begins. Running SUM of these flags creates merge group IDs. TutorialsPoint - Merge Overlapping Events in the Same Hall | Optimal Solution
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