The Roads In

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Airbnb

Problem

The content marketing team is auditing where blog traffic originates. From the page views whose URL contains '/blog', pull the unique referrer values.

Every reader arrives from somewhere. Trace where the blog's traffic begins.

Tables

page_views

Columns: view_id INTEGER, page_url TEXT, user_id INTEGER, referrer TEXT, dur_ms INTEGER, device TEXT, viewed_at TEXT

page_views: 10 of 200 rows
view_idpage_urluser_idreferrerdur_msdeviceviewed_at
5053/products1555amazon.com637desktop2026-09-02 01:09:00
5106/checkout1652twitter.com774tablet2026-09-03 02:18:00
5159/profile1749github.com911Mobile2026-09-04 03:27:00
5212/search1846NULL1048DESKTOP2026-09-05 04:36:00
5265/about1943reddit.com1185mobile2026-09-06 05:45:00
5318/blog/intro1555NULLNULLdesktop2026-09-07 06:54:00
5371/pricing1652bing.com1459tablet2026-09-08 07:03:00
5424/docs/api1749news.ycombinator.com1596Mobile2026-09-09 08:12:00
5477/settings1846NULL1733DESKTOP2026-09-10 09:21:00
5530/Home1943bing.com1870mobile2026-09-11 10:30:00

Expected output

First 4 of 4 rows
referrer
NULL
bing.com
news.ycombinator.com
twitter.com

Worked solution and explanation

What this problem really is

Strip the traffic-analysis costume and this is a filter-then-dedup problem: match the blog URLs, then collapse the referrer column to its unique values. Everyone writes the pattern match. The tell is the leading wildcard: anchor the pattern to the start of the string and you silently drop every real-world URL like /content/blog/article, so the referrer list comes back short with no error to warn you. The second trap is the NULL referrer. It is the largest slice of blog traffic, SELECT DISTINCT keeps it as one row, and a habitual WHERE referrer IS NOT NULL erases direct traffic from the audit.


Break down the requirements

Step 1: Filter to blog pages

WHERE page_url LIKE '%/blog%' keeps any URL where '/blog' appears anywhere in the path. The leading % is what catches nested paths, not just those that start with /blog.

Step 2: Deduplicate referrer

SELECT DISTINCT referrer collapses the surviving rows to one row per referrer value. NULL survives as a single distinct row, standing in for direct traffic.


The solution

URL-filtered distinct referrers
SELECT DISTINCT referrer
FROM page_views
WHERE page_url LIKE '%/blog%'

Common follow-up questions