Human Traffic of Stadium - Problem

You have a Stadium table that records the daily visit information to a stadium. Each row contains the visit date, visit id, and number of people who visited on that date.

Write a SQL solution to display records where:

  • There are three or more consecutive rows with consecutive IDs
  • Each of these rows has people ≥ 100

Return the result table ordered by visit_date in ascending order.

Table Schema

Stadium
Column Name Type Description
id PK int Visit ID (increases with date)
visit_date date Date of the visit (unique)
people int Number of people who visited
Primary Key: id
Note: As the id increases, the date increases as well. Each visit_date is unique.

Input & Output

Example 1 — Mixed Busy and Quiet Days
Input Table:
id visit_date people
1 2017-01-01 10
2 2017-01-02 109
3 2017-01-03 150
4 2017-01-04 99
5 2017-01-05 145
6 2017-01-06 1455
7 2017-01-07 199
8 2017-01-08 188
Output:
id visit_date people
5 2017-01-05 145
6 2017-01-06 1455
7 2017-01-07 199
8 2017-01-08 188
💡 Note:

The stadium has records with IDs 5, 6, 7, 8 that are consecutive and each has at least 100 people (145, 1455, 199, 188). This forms a group of 4 consecutive busy days, which meets the requirement of 3 or more.

Example 2 — No Qualifying Sequences
Input Table:
id visit_date people
1 2017-01-01 10
2 2017-01-02 109
3 2017-01-03 150
4 2017-01-04 99
5 2017-01-05 145
Output:
id visit_date people
💡 Note:

Although some days have 100+ people, there's no sequence of 3 or more consecutive days where each day has at least 100 people. IDs 2,3 have 100+ but that's only 2 consecutive days.

Constraints

  • 1 ≤ id ≤ 1000
  • visit_date is unique for each row
  • 0 ≤ people ≤ 10000
  • As id increases, visit_date increases as well

Visualization

Tap to expand
Human Traffic of Stadium INPUT: Stadium Table id visit_date people 1 2017-01-01 10 2 2017-01-02 109 3 2017-01-03 150 4 2017-01-04 99 5 2017-01-05 145 6 2017-01-06 1455 7 2017-01-07 199 8 2017-01-09 188 people >= 100 people < 100 Condition: 3+ consecutive ids with people >= 100 each ALGORITHM STEPS 1 Filter rows with people>=100 Keep only high-traffic visits 2 Find consecutive sequences Use id - row_number trick id: 2,3,5,6,7,8 rn: 1,2,3,4,5,6 grp: 1,1,2,2,2,2 (id - row_num = group) 3 Count per group Keep groups with count>=3 grp 1: 2 rows (REJECTED) grp 2: 4 rows (OK) 4 Return qualifying rows Order by visit_date FINAL RESULT id visit_date people 5 2017-01-05 145 6 2017-01-06 1455 7 2017-01-07 199 8 2017-01-09 188 4 Consecutive High-Traffic IDs: 5, 6, 7, 8 All have people >= 100 Consecutive Sequence: 5 6 7 8 --> --> --> Key Insight: The id - ROW_NUMBER() trick creates groups: consecutive IDs get the same difference value. For example: IDs 5,6,7,8 with row numbers 3,4,5,6 all give difference = 2, forming one group. This allows us to use GROUP BY and HAVING COUNT(*) >= 3 to find valid sequences efficiently. TutorialsPoint - Human Traffic of Stadium | Optimal Solution (Window Functions)
Asked in
Amazon 12 Google 8 Microsoft 6
28.0K 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