SQL IS NULL Practice: Finding Anonymous Web Sessions
In this dataset, a missing user_id represents a session that wasn't tied to a signed-in user.
Not every visit is tied to a known person. In this dataset, sessions that
aren't tied to a signed-in user have a missing
user_id, represented as
NULL. Counting and inspecting those anonymous sessions is a routine analytics
task.
Remember that
NULL
is a missing/unknown marker, so ordinary equality comparisons with it aren't
true. Writing
user_id = NULL
would match zero rows and give you an empty report with no error.
Test missing values with IS NULL
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 unshipped orders. It doesn't change just because
the dataset does.
select columns
from table_name
where nullable_column is null
order by sort_column Schema · Website Analytics Table · sessions 7 columns · 30 rows
One row per visit. Anonymous visits have a null user_id; organic and direct visits have a null campaign_id.
List every anonymous session, meaning one with no user id. Return the session id, session date, channel, and device, oldest first.
Run your query to inspect the result.
Expected answer
- Columns: session_id, session_date, channel, device.
- Rows: 13 anonymous sessions.
- The earliest is session 14 on 2024-01-01 (direct, desktop).
Related
IS NULL, COALESCE, NULLIF, and null-safe logic.
IS NULL, COALESCE, NULLIF, and null-safe equality.
Use IS NULL instead of equality.
A stray NULL drops every row. Use NOT EXISTS.
COUNT(*) counts rows; COUNT(column) counts non-NULL values.
Find missing values with IS NULL.