Popular Exhibit-to-Exhibit Routes
Problem
A science museum logs each exhibit a visitor finishes in the table `exhibit_visits`, with the `visitor_id`, the `exhibit_id`, the `exhibit_name`, the `visited_at` timestamp, and the `enjoyment` score (1 to 5) the visitor gave that exhibit. The curators want walking routes from the museum's *enthusiasts* only: visitors who finished at least 5 exhibits AND whose average enjoyment is at least 4. For those visitors, look at each pair of exhibits visited back-to-back (the exhibit visited immediately after another, in time order), and count how often each such ordered pair occurs across all enthusiasts. Return `from_exhibit`, `to_exhibit`, and the occurrence count as `route_count`. Order by `route_count` descending, then by `from_exhibit` ascending, then by `to_exhibit` ascending.
Tables
Example rows — the live problem includes the full dataset.
| visitor_id | exhibit_id | exhibit_name | visited_at | enjoyment |
|---|---|---|---|---|
| 1 | 11 | Gravity Lab | 2024-01-05 | 5 |
| 1 | 12 | Tide Pool | 2024-01-06 | 4 |
| 1 | 13 | Star Dome | 2024-01-07 | 5 |
Expected output
Your answer should return 5 rows with the columns from_exhibit, to_exhibit, route_count.
Starter code (SQL)
SELECT *
FROM exhibit_visits;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock