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

The infrastructure team wants each edge location compared to the global baseline. For each edge, show its request count, its local cache-hit percentage, and the overall cache-hit percentage across all edges repeated on every row for easy comparison, running from the best-caching edge down to the worst.

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 INTEGER, served_at TEXT

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

Why this problem exists in real interviews

Built around cdn_logs, this problem requires conditional branching with CASE combined with division-safe NULLIF guards. Interviewers watch whether candidates handle the relationship between edge_loc, req_path, status correctly under grouping pressure.


Break down the requirements

Step 1: Group by edge_loc

Read the prose literally: one row per edge_loc carrying (a) its request_count, (b) its LOCAL cache-hit percentage, and (c) the GLOBAL cache-hit percentage repeated on every row so each edge can be eyeballed against the baseline. cache_hit is a 0/1 flag, so SUM(cache_hit) counts hits and COUNT(*) counts requests.

Step 2: Compute the ratio with CASE and NULLIF

Build the per-edge layer with GROUP BY edge_loc: request_count = COUNT(*), and the local rate = 100.0 * SUM(cache_hit) / COUNT(*). The 100.0 forces float division (avoids SQLite integer truncation) and SUM(cache_hit) is exact because the flag is a clean 0/1 with no NULLs.

Step 3: Round and order

Identify the real trap for the global baseline: 'overall across all edges' must be the POOLED rate = (total hits over the whole table) / (total requests over the whole table), NOT the unweighted average of the eight per-edge percentages. Because the edge groups have unequal sizes (24 vs 26 rows), averaging per-edge rates is the wrong, weight-blind answer.


The solution

Case-branch for cache efficiency
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;

Common follow-up questions