# The Roads In

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

Canonical URL: <https://datadriven.io/problems/the_roads_in>

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.

## Tables

### page_views

- view_id INTEGER
- page_url TEXT
- user_id INTEGER
- referrer TEXT
- dur_ms INTEGER
- device TEXT
- viewed_at TEXT

Sample rows (10 of 200):

| view_id | page_url | user_id | referrer | dur_ms | device | viewed_at |
| --- | --- | --- | --- | --- | --- | --- |
| 5053 | /products | 1555 | amazon.com | 637 | desktop | 2026-09-02 01:09:00 |
| 5106 | /checkout | 1652 | twitter.com | 774 | tablet | 2026-09-03 02:18:00 |
| 5159 | /profile | 1749 | github.com | 911 | Mobile | 2026-09-04 03:27:00 |
| 5212 | /search | 1846 | NULL | 1048 | DESKTOP | 2026-09-05 04:36:00 |
| 5265 | /about | 1943 | reddit.com | 1185 | mobile | 2026-09-06 05:45:00 |
| 5318 | /blog/intro | 1555 | NULL | NULL | desktop | 2026-09-07 06:54:00 |
| 5371 | /pricing | 1652 | bing.com | 1459 | tablet | 2026-09-08 07:03:00 |
| 5424 | /docs/api | 1749 | news.ycombinator.com | 1596 | Mobile | 2026-09-09 08:12:00 |
| 5477 | /settings | 1846 | NULL | 1733 | DESKTOP | 2026-09-10 09:21:00 |
| 5530 | /Home | 1943 | bing.com | 1870 | mobile | 2026-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**

```sql
SELECT DISTINCT referrer
FROM page_views
WHERE page_url LIKE '%/blog%'
```

> **One full scan, then a tiny result**
>
> This is a scan of 700M rows with a `LIKE` filter. The leading wildcard keeps any index on `page_url` from being used, so it is a full scan. `DISTINCT` then reduces the output to a handful of unique referrers, which is cheap relative to the scan.

> **Substring versus prefix is the tell**
>
> Interviewers watch whether the candidate writes `LIKE '%/blog%'` (substring) or `LIKE '/blog%'` (prefix). The prompt says the URL contains '/blog', so the leading wildcard is required; a prefix pattern is the fast way to look right and be wrong.

> **A prefix pattern runs clean and answers wrong**
>
> Dropping the leading `%` matches only URLs that begin with /blog and misses paths like /content/blog/article. The query still runs and still returns rows, so the mistake ships as an undercounted referrer list rather than an error.

---

## Common follow-up questions

- How would you also count views per referrer? _(Replace `DISTINCT` with `GROUP BY referrer` and add `COUNT(*)`. The `NULL` group still appears.)_
- What if you only want external referrers and need to drop the direct-traffic nulls? _(`NULL` appears as a distinct value here; tests whether the candidate knows to exclude it deliberately for external-only sources.)_
- How would you make this fast on a 700M-row table? _(Tests partial indexes, a normalized `referrer_domain` column, materialized views, or pre-filtering strategies.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_roads_in)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.