Exposure Rate of Violating Content: Ex-ante vs Ex-post Views
SQL & DataModel answerLast updated
Write SQL that labels each view by the moderation decision known at view time and by the final decision, computes daily prevalence per country, and ranks the top creators by violating share.
You work on analytics for a content platform and need to measure how often people see policy-violating content. Three tables are available:
content_views(view_ts TIMESTAMP, user_id INT, content_id INT, surface VARCHAR, country VARCHAR)content_moderation(content_id INT, decision VARCHAR, violation_type VARCHAR, detected_ts TIMESTAMP, source VARCHAR, is_removed BOOLEAN)content(content_id INT, creator_id INT, created_ts TIMESTAMP, country VARCHAR)
A content item can have several moderation rows over time, for example a VIOLATION later overturned by a CLEAN.
Treat "today" as 2025-09-01. The reporting window is the last 7 days, 2025-08-26 through 2025-09-01 inclusive.
Part 1: daily prevalence. For each (view_date, country), where country is the viewer's country from content_views, return:
view_date, country, total_views, violating_views_ex_ante, violating_views_ex_post, ex_ante_prevalence, ex_post_prevalence
- Ex-ante: a view is violating only if the most recent decision with
detected_ts <= view_tsisVIOLATION. If the content has no decision at that moment, treat it as CLEAN. - Ex-post: a view is violating if the content's latest decision overall (any
detected_ts) isVIOLATION. - Prevalence is violating views divided by total views.
Part 2: worst creators. Over the same window, find the top 3 creators by ex-post violating_view_share, defined as the creator's ex-post violating views divided by all ex-post violating views in the window. For each of them, break their violating views down by surface.
Sample data:
content
| content_id | creator_id | created_ts | country |
|---|---|---|---|
| 101 | 1001 | 2025-08-25 10:00:00 | US |
| 102 | 1002 | 2025-08-30 12:00:00 | US |
| 103 | 1003 | 2025-08-31 08:00:00 | CA |
content_views
| view_ts | user_id | content_id | surface | country |
|---|---|---|---|---|
| 2025-08-26 09:00:00 | 504 | 101 | feed | US |
| 2025-08-30 18:00:00 | 501 | 102 | feed | US |
| 2025-08-31 20:00:00 | 501 | 101 | feed | US |
| 2025-09-01 09:00:00 | 502 | 102 | feed | US |
| 2025-09-01 10:00:00 | 503 | 103 | search | CA |
content_moderation
| content_id | decision | violation_type | detected_ts | source | is_removed |
|---|---|---|---|---|---|
| 101 | VIOLATION | spam | 2025-08-31 21:00:00 | human | true |
| 102 | CLEAN | NULL | 2025-08-30 14:00:00 | model | false |
| 103 | VIOLATION | hate_speech | 2025-09-01 12:00:00 | model | true |
Write the SQL (any mainstream dialect, PostgreSQL preferred), explain the choices that affect correctness, and state what your queries return on the sample.
Hints
Hint 1
Treat each view as the unit of analysis. Prevalence counts views, not distinct content items, so every view needs its own ex-ante label and ex-post label.
Hint 2
The ex-ante label is an as-of lookup: for each view, take the newest moderation row at or before view_ts. A LEFT JOIN LATERAL with ORDER BY ... LIMIT 1 (or ROW_NUMBER over a range join) does this, and a missing row maps to CLEAN.
Hint 3
Filter the window with view_ts >= '2025-08-26' and view_ts < '2025-09-02'. BETWEEN with a date literal on the upper end silently drops most of the last day. Also break ties on identical detected_ts explicitly.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Prevalence is a view-weighted ratio. The numerator is violating views and the denominator is all views, so the grain of the working set is one row per view, carrying two flags:
- Ex-ante flag: was the content already known to be violating at the moment it was viewed? This is an as-of lookup against
content_moderation. - Ex-post flag: is the content violating according to its latest decision overall? This is a plain "latest row per content" lookup.
The gap between the two measures exposure that happened before enforcement caught up.
Steps:
- Restrict views to the window using a half-open range.
- Find the latest decision per content (ex-post).
- For each view, find the latest decision with
detected_ts <= view_ts(ex-ante). No match means CLEAN. - Group by day and viewer country for Part 1.
- Reuse the labeled views for Part 2: sum ex-post violating views per creator, compute the share, keep the top 3, and split by surface.
Correctness points
- Window boundary. Use
view_ts >= '2025-08-26' AND view_ts < '2025-09-02'.BETWEEN '2025-08-26' AND '2025-09-01'would treat the upper bound as midnight and lose almost all of 09-01. - No row multiplication. Both lookups return at most one row per view (
LIMIT 1orDISTINCT ON), soCOUNT(*)equals the real view count. Joining views to all moderation rows would inflate counts for content with multiple decisions. - Missing decisions. The LEFT JOIN yields NULL when nothing qualifies, and a
CASEmaps NULL to 0, which matches "assume CLEAN". - Overturned decisions. Because the lookup takes the newest qualifying row, a VIOLATION later reversed by CLEAN stops counting from the reversal onward.
- Ties. If two rows share a
detected_ts, order by "VIOLATION first" so the result is deterministic and conservative. - Country. Group on
content_views.country(the viewer), notcontent.country(the content's origin).
Part 1 (PostgreSQL)
WITH win_views AS (
SELECT view_ts, content_id, surface, country
FROM content_views
WHERE view_ts >= TIMESTAMP '2025-08-26 00:00:00'
AND view_ts < TIMESTAMP '2025-09-02 00:00:00'
),
latest_overall AS (
SELECT DISTINCT ON (content_id) content_id, decision
FROM content_moderation
ORDER BY content_id,
detected_ts DESC,
(decision = 'VIOLATION') DESC
),
labeled AS (
SELECT v.view_ts, v.content_id, v.surface, v.country,
CASE WHEN a.decision = 'VIOLATION' THEN 1 ELSE 0 END AS viol_ante,
CASE WHEN f.decision = 'VIOLATION' THEN 1 ELSE 0 END AS viol_post
FROM win_views v
LEFT JOIN LATERAL (
SELECT m.decision
FROM content_moderation m
WHERE m.content_id = v.content_id
AND m.detected_ts <= v.view_ts
ORDER BY m.detected_ts DESC,
(m.decision = 'VIOLATION') DESC
LIMIT 1
) a ON TRUE
LEFT JOIN latest_overall f ON f.content_id = v.content_id
)
SELECT view_ts::date AS view_date,
country,
COUNT(*) AS total_views,
SUM(viol_ante) AS violating_views_ex_ante,
SUM(viol_post) AS violating_views_ex_post,
ROUND(SUM(viol_ante)::numeric / COUNT(*), 4) AS ex_ante_prevalence,
ROUND(SUM(viol_post)::numeric / COUNT(*), 4) AS ex_post_prevalence
FROM labeled
GROUP BY 1, 2
ORDER BY 1, 2;
Part 2
State the definition explicitly: violating_view_share = creator's ex-post violating views / all ex-post violating views in the window. This ranks creators by how much of the total harmful exposure they drive. The alternative reading (creator violating views / creator total views) is a per-creator rate; if you choose it, add a minimum-views threshold, otherwise a creator with one view and one violation tops the list at 100%.
Append to the same WITH chain (compute the share before limiting to 3, so the denominator is the global total):
, per_creator AS (
SELECT c.creator_id,
SUM(l.viol_post) AS violating_views
FROM labeled l
JOIN content c ON c.content_id = l.content_id
GROUP BY c.creator_id
HAVING SUM(l.viol_post) > 0
),
top3 AS (
SELECT creator_id,
violating_views,
violating_views::numeric / SUM(violating_views) OVER () AS violating_view_share
FROM per_creator
ORDER BY violating_views DESC, creator_id
LIMIT 3
)
SELECT t.creator_id,
ROUND(t.violating_view_share, 4) AS violating_view_share,
l.surface,
COUNT(*) AS violating_views_on_surface
FROM top3 t
JOIN content c ON c.creator_id = t.creator_id
JOIN labeled l ON l.content_id = c.content_id
AND l.viol_post = 1
GROUP BY t.creator_id, t.violating_view_share, t.violating_views, l.surface
ORDER BY t.violating_views DESC, t.creator_id, violating_views_on_surface DESC;
Note: the share must be computed in a CTE where SUM() OVER () still sees every creator. Applying LIMIT 3 in the same SELECT as the window function is fine, because window functions are evaluated before LIMIT.
Result on the sample
| view | ex-ante | ex-post |
|---|---|---|
| 08-26 09:00, content 101, US | CLEAN (decision arrives 08-31 21:00) | VIOLATION |
| 08-30 18:00, content 102, US | CLEAN | CLEAN |
| 08-31 20:00, content 101, US | CLEAN (decision is one hour later) | VIOLATION |
| 09-01 09:00, content 102, US | CLEAN | CLEAN |
| 09-01 10:00, content 103, CA | CLEAN (decision at 12:00) | VIOLATION |
| view_date | country | total | ex_ante | ex_post | ex_ante_prev | ex_post_prev |
|---|---|---|---|---|---|---|
| 2025-08-26 | US | 1 | 0 | 1 | 0.0000 | 1.0000 |
| 2025-08-30 | US | 1 | 0 | 0 | 0.0000 | 0.0000 |
| 2025-08-31 | US | 1 | 0 | 1 | 0.0000 | 1.0000 |
| 2025-09-01 | CA | 1 | 0 | 1 | 0.0000 | 1.0000 |
| 2025-09-01 | US | 1 | 0 | 0 | 0.0000 | 0.0000 |
Ex-ante is zero everywhere because every violating view happened before the content was flagged.
Creators: 1001 has share 2/3 ≈ 0.6667 (feed: 2 views); 1003 has share 1/3 ≈ 0.3333 (search: 1 view). Creator 1002 has no violating views and is excluded.
Complexity
The as-of lookup is roughly O(V log M) with an index on content_moderation (content_id, detected_ts DESC), where V is the number of views in the window and M the moderation rows per content (typically very few). Without that index the LATERAL subquery scans per view, so add it for production volumes. Indexing content_views (view_ts) keeps the window scan cheap.
Common mistakes
- Using
BETWEENwith a bare date as the upper bound. - Joining views to every moderation row, which duplicates views.
- Counting distinct content instead of views.
- Using the content's country instead of the viewer's.
- Computing the share after
LIMIT 3, which makes the top three always sum to 100%. - Forgetting that a later CLEAN can override an earlier VIOLATION.