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

SQL IS NULL Practice: Finding Anonymous Web Sessions

A missing user_id is meaningful: it marks a visitor who was not signed in.

Not every visit is tied to a known person. When a visitor is not signed in, the session's user_id is NULL. Counting and inspecting those anonymous sessions is a routine analytics task.

Remember that NULL means "unknown", so it is never equal to anything. Writing user_id = NULL would match zero rows and give you a silently empty report.

IS NULL is the only reliable test

Use IS NULL to find missing values and IS NOT NULL to exclude them. This is the same rule you met with unreturned rentals and with the Garden Shop's empty shelves. It does not change just because the dataset does.

Pattern
select columns
from table_name
where nullable_column is null
order by sort_column
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

List every anonymous session, meaning one with no user id. Return the session id, session date, channel, and device, oldest first.

SQL Workbench
query.sqlwebsite_analytics · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: session_id, session_date, channel, device.
  • Rows: 13 anonymous sessions.
  • The earliest is session 14 on 2024-01-01 (direct, desktop).