CDN Image Request Paths
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
- City and County of San Francisco
Problem
We're auditing image delivery across our CDN, and any request whose path includes 'image' counts as image traffic. Pull the edge location and request path of every such entry in cdn_logs, repeat requests included.
CDN image traffic. Every path.
Tables
cdn_logs
Columns: log_id INTEGER, edge_loc TEXT, req_path TEXT, status INTEGER, bytes INTEGER, cache_hit INTEGER, served_at TEXT
| log_id | edge_loc | req_path | status | bytes | cache_hit | served_at |
|---|---|---|---|---|---|---|
| 537 | SFO | /js/app.min.js | 200 | 1437 | 0 | 2026-02-02 01:07:13 |
| 574 | LHR | /css/main.css | 200 | 2774 | 0 | 2026-03-03 02:14:26 |
| 611 | NRT | /images/hero.jpg | 206 | 4111 | 1 | 2026-04-04 03:21:39 |
| 648 | SIN | /api/v1/data | 301 | 5448 | 0 | 2026-05-05 04:28:52 |
| 685 | FRA | /fonts/roboto.woff2 | 304 | NULL | 0 | 2026-06-06 05:35:05 |
| 722 | SYD | /video/intro.mp4 | 403 | 8122 | NULL | 2026-07-07 06:42:18 |
| 759 | GRU | /favicon.ico | 404 | 9459 | NULL | 2026-08-08 07:49:31 |
| 796 | IAD | /assets/logo.png?v=2 | 500 | 10796 | NULL | 2026-09-09 08:56:44 |
| 833 | SFO | /js/app.min.js | 503 | 12133 | NULL | 2026-01-10 09:03:57 |
| 870 | LHR | /assets/logo.png | 200 | 13470 | 0 | 2026-02-11 10:10:10 |
Expected output
| edge_loc | req_path |
|---|---|
| NRT | /images/hero.jpg |
| FRA | /images/hero.jpg |
| GRU | /images/hero.jpg |
| SFO | /images/hero.jpg |
| NRT | /images/hero.jpg |
Worked solution and explanation
This is an unanchored substring filter in a CDN costume. Every value in req_path starts with '/', so 'image' is never the whole path and never its first characters: it sits inside '/images/hero.jpg'. Anyone can type LIKE. What separates candidates is where the wildcards go. Write = 'image' or LIKE 'image%' and the query runs cleanly, returns zero rows, and the team concludes there is no image traffic at all.
Getting there
Step 1: Read the paths before writing the pattern
Look at req_path in the sample rows: '/images/hero.jpg' opens with a slash and continues past the word. The token is embedded, so the pattern needs a % on both sides: req_path LIKE '%image%'.
Step 2: Keep every log line
The ask is every entry, and one edge serves the same path many times. Leave out DISTINCT: the 20 matching rows include 'NRT' with '/images/hero.jpg' more than once, and each of those is a separate request.
Step 3: Project only edge_loc and req_path
Those are the two columns asked for. Nothing in the question asks for an order, so none is imposed.
SELECT edge_loc, req_path
FROM cdn_logs
WHERE req_path LIKE '%image%'- Anchored pattern
LIKE 'image%'asks whether the path begins with 'image'. Everyreq_pathbegins with '/', so it returns 0 rows.- Unanchored pattern
LIKE '%image%'asks whether 'image' appears anywhere, and it matches all 20 '/images/hero.jpg' requests.
Common follow-up questions
- How would you make the filter case-insensitive? (Tests `ILIKE` or wrapping the column in `LOWER()`.)
- What index type would help with leading-wildcard `LIKE` queries? (Tests knowledge of trigram (GIN) indexes for substring search.)
- What if you needed just the file extension from `req_path`? (Tests string functions like `SPLIT_PART` or `REGEXP_SUBSTR`, and handling of suffixes like '?v=2'.)