Cache Efficiency
A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- hard
- Seniority
- L5
Problem
cdn_logs holds one row per request an edge location served, with cache_hit set to 1 when the edge answered from cache. List each edge's request count and cache-hit percentage beside the network-wide hit percentage over every request, the same baseline repeated on each row, best-caching edge first.
Some edges run hot. Others coast on the global average.
Tables
cdn_logs
Columns: log_id INTEGER, edge_loc TEXT, req_path TEXT, status INTEGER, bytes INTEGER, cache_hit BOOLEAN, served_at TIMESTAMP
| 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 | request_count | hit_pct | overall_hit_pct |
|---|---|---|---|
| FRA | 24 | 25 | 19 |
| NRT | 26 | 23.08 | 19 |
| SIN | 26 | 23.08 | 19 |
| GRU | 24 | 16.67 | 19 |
| IAD | 24 | 16.67 | 19 |
Worked solution and explanation
What this problem really is
Beneath the CDN framing, this asks for 2 aggregates at 2 grains in one pass: each edge's own hit rate, and the network-wide rate stamped onto every edge row. Everyone gets the GROUP BY edge_loc right. The real test is the baseline. It is the ratio of global totals, with every request weighted once. Average the 8 per-edge rates instead and a quiet edge pulls the baseline as hard as a busy one, so every row is compared against the wrong number.
The baseline is meant to be identical on every row. It is one rate, 38 hits over 200 requests on this data, repeated so each edge can be read against it; it never adds up to 100 across edges. A column of 19.0 down the result means the query is working.
Building it
Step 1: Count every request per edge_loc
The request count is COUNT(*). cache_hit is empty on 80 of the 200 rows: every request with a status of 400 or above. Those requests were still served, so they belong in the denominator, and COUNT(cache_hit) would drop them. SUM(cache_hit) skips them, so they never count as hits.
Step 2: Derive the local rate from the same group
hit_pct is 100.0 * SUM(cache_hit) / COUNT(*), rounded to 2 places. The 100.0 scales the ratio to a percentage and keeps the division decimal. With an integer 100 and 2 integer aggregates, many engines truncate 'NRT' at 23.08 down to 23.
Step 3: Pool the totals for the baseline
Divide total hits by total requests, both summed over the grouped rows with an empty OVER (). Do not average hit_pct. Then sort by hit_pct descending. 'NRT' and 'SIN' tie at 23.08, and either may come first.
SELECT
edge_loc,
COUNT(*) AS request_count,
ROUND(100.0 * SUM(cache_hit) / COUNT(*), 2) AS hit_pct,
ROUND(
100.0 * SUM(SUM(cache_hit)) OVER ()
/ SUM(COUNT(*)) OVER (),
2
) AS overall_hit_pct
FROM cdn_logs
GROUP BY edge_loc
ORDER BY hit_pct DESC;One scan of cdn_logs; the window only touches the grouped rows.
- Mean of rates (wrong)
AVG(hit_pct)over the 8 rows gives each edge one vote. Here it lands on 18.99, because the 26-request edges count no more than the 24-request ones. On production traffic, whereedge_locis heavily skewed, the gap is far wider.- Ratio of totals (right)
SUM(SUM(cache_hit)) OVER () / SUM(COUNT(*)) OVER ()weights each edge by its traffic. That makes it exactly 38 hits over 200 requests: 19.0.
Common follow-up questions
- Show each edge's gap from the baseline and keep only edges more than 5 points below it. (Tests filtering on a window result, which needs a CTE or `QUALIFY` because `WHERE` runs before the window.)
- Compute the baseline per region instead of globally, given a `region` column. (Tests turning `OVER ()` into `OVER (PARTITION BY region)` over the grouped rows.)
- What changes if `edge_loc` has trailing whitespace or mixed casing? (Tests normalizing with `TRIM` and `UPPER` before grouping, so one edge is not split into several groups.)
- Limit this to the last 7 days of `served_at` without scanning all 1,095 partitions. (Tests putting the filter on the partition column so the engine prunes partitions before aggregating.)