SQL Top-N Practice: A Most-Viewed Pages Report
The closing capstone: group pageviews by path, rank them, and keep the top five.
Every row in pageviews is one page load, tagged with the path that was viewed. "What are our most popular pages?" is a Top-N report. The same pattern you used for the most-rented movies, on a new table.
Group by path, count the views, rank them, and keep only the leaders with LIMIT.
The tie-breaker earns its keep
Several paths are viewed the same number of times, so where the LIMIT cut lands depends on the sort. Ordering by views and then path makes the Top-5 stable and repeatable, a fitting way to close out the course.
select group_column, count(*) as total
from table_name
group by group_column
order by total desc, group_column
limit 5Schema · Website AnalyticsTable · pageviews4 columns · 75 rows
One row per page viewed within a session, in view_order.
Build a Top-5 most-viewed pages report: return eachpath and its number of views. Rank by views descending, break ties by path, and keep only the top five.
- Columns: path, views.
- Rows: 5, the most-viewed paths.
- The home page (/) leads with 26 views.
You finished “Applied Practice: Website Analytics.”
Nice work. Ready to start the next one?
Start Chapter 13: Applied Practice: Store Operations →Begins with 13.1 Daily revenue report