Skip to content
/Chapter 12 · Applied Practice: Website Analytics
Lesson 12.2·website_analytics
Lesson 12.2

SQL Practice: Filtering the Longest Web Sessions

Not every question is a GROUP BY. Sometimes you just want the individual standout rows.

Aggregates summarize, but sometimes you want the raw rows themselves. The specific sessions where a visitor stuck around. That is a plain WHERE filter, no grouping involved.

Here "engaged" is defined as a session lasting at least 400 seconds. The comparison duration_seconds >= 400 keeps only those rows, and sorting brings the longest to the top.

A stable sort needs a tie-breaker

Two sessions could share the same duration. Adding session_id as a second sort key means the ranking is identical on every run. The same habit that keeps LIMIT results deterministic.

Pattern
select columns
from table_name
where number_column >= threshold
order by number_column desc, tie_breaker
Schema · Website AnalyticsTable · sessions7 columns · 30 rows
Table · sessions

One row per visit. Anonymous visits have a null user_id; organic and direct visits have a null campaign_id.

7 columns · 30 rows
session_id intuser_id intsession_date datechannel textcampaign_id intdevice textduration_seconds int
Your task

Return the session id, channel, device, and duration for every session that lasted 400 seconds or longer, longest first. Break ties by session id.

SQL Workbench
query.sqlwebsite_analytics · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: session_id, channel, device, duration_seconds.
  • Rows: 8 sessions lasting 400 seconds or longer.
  • The longest is session 22 (paid, desktop) at 600 seconds.