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

cdn_logs: 10 of 200 rows
log_idedge_locreq_pathstatusbytescache_hitserved_at
537SFO/js/app.min.js200143702026-02-02 01:07:13
574LHR/css/main.css200277402026-03-03 02:14:26
611NRT/images/hero.jpg206411112026-04-04 03:21:39
648SIN/api/v1/data301544802026-05-05 04:28:52
685FRA/fonts/roboto.woff2304NULL02026-06-06 05:35:05
722SYD/video/intro.mp44038122NULL2026-07-07 06:42:18
759GRU/favicon.ico4049459NULL2026-08-08 07:49:31
796IAD/assets/logo.png?v=250010796NULL2026-09-09 08:56:44
833SFO/js/app.min.js 50312133NULL2026-01-10 09:03:57
870LHR/assets/logo.png2001347002026-02-11 10:10:10

Expected output

First 5 of 8 rows
edge_locrequest_counthit_pctoverall_hit_pct
FRA242519
NRT2623.0819
SIN2623.0819
GRU2416.6719
IAD2416.6719

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.

Per-edge rate beside the pooled baseline
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, where edge_loc is 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