Approved-Profile Daily Snapshot with Country Rollup and Checks
SQL & DataModel answerLast updated
Build a point-in-time snapshot of profiles whose latest version has an approved final review, add 7-day engagement and country rollups, and write data-quality and rerun-safety checks.
Scenario
A dating app wants a daily report on profile quality and engagement. The report runs for the snapshot date 2025-09-01, and all timestamps are UTC. A profile belongs in the report only if both of these hold:
- Its newest version (as of
2025-09-01 00:00:00) has a final review withstatus = 'approved'made on or before that moment. - The profile is not deleted (
is_deleted = 0).
Schema and sample data
users
| id | signup_date | country |
|---|---|---|
| 1 | 2025-08-20 | US |
| 2 | 2025-08-25 | US |
| 3 | 2025-08-28 | CA |
| 4 | 2025-07-15 | US |
profiles
| profile_id | user_id | created_at | is_deleted |
|---|---|---|---|
| 101 | 1 | 2025-08-21 10:00:00 | 0 |
| 102 | 2 | 2025-08-25 09:00:00 | 0 |
| 103 | 3 | 2025-08-28 12:30:00 | 1 |
| 104 | 4 | 2025-07-16 08:15:00 | 0 |
profile_versions
| version_id | profile_id | created_at | bio_text | photos_count |
|---|---|---|---|---|
| 1001 | 101 | 2025-08-21 10:05:00 | Hi | 2 |
| 1002 | 101 | 2025-08-29 07:00:00 | Hello | 3 |
| 1003 | 102 | 2025-08-25 09:05:00 | Traveler | 1 |
| 1004 | 103 | 2025-08-28 12:35:00 | Foodie | 0 |
| 1005 | 104 | 2025-08-20 18:00:00 | Hiker | 4 |
profile_reviews
| review_id | profile_id | version_id | status | reviewed_at | is_final |
|---|---|---|---|---|---|
| 5001 | 101 | 1001 | approved | 2025-08-21 12:00:00 | 0 |
| 5002 | 101 | 1002 | approved | 2025-08-29 12:00:00 | 1 |
| 5003 | 102 | 1003 | rejected | 2025-08-25 10:00:00 | 1 |
| 5004 | 103 | 1004 | approved | 2025-08-28 14:00:00 | 1 |
| 5005 | 104 | 1005 | pending | 2025-08-20 19:00:00 | 0 |
profile_events
| id | profile_id | event_type | event_time |
|---|---|---|---|
| 9001 | 101 | view | 2025-08-31 10:00:00 |
| 9002 | 101 | like | 2025-08-31 10:05:00 |
| 9003 | 102 | view | 2025-08-30 09:30:00 |
| 9004 | 104 | view | 2025-08-26 12:00:00 |
| 9005 | 104 | like | 2025-08-31 14:00:00 |
Assume the text values are stored without the display quotes.
Rules
- The latest version of a profile is the one with the greatest
created_atthat is<= 2025-09-01 00:00:00. - The engagement window covers the 7 days from
2025-08-26 00:00:00through the end of2025-09-01(inclusive of that whole day).
Tasks
- Snapshot table. Write SQL (CTEs and window functions) that produces one row per qualifying profile with:
user_id,country,profile_id,latest_version_id,approved_at,photos_count,bio_len,last_7d_views,last_7d_likes,last_7d_like_rate(likes divided by views). - Country rollup. From the snapshot, compute per country:
profiles_approved,pct_with_photo(photos_count >= 1),pct_with_bio_10+(LENGTH(bio_text) >= 10),last_7d_views,last_7d_likes,last_7d_like_rate. - Validation queries.
- (a) Flag profiles whose final review points at a version that is not the latest.
- (b) Detect profiles with more than one final review.
- (c) Surface profiles that received events after the snapshot time.
- (d) Show how the job stays idempotent when rerun, by bounding everything to the snapshot.
Briefly explain any ambiguity you resolve and how the sample data comes out.
Hints
Hint 1
Treat each table separately: pick the newest version with created_at <= snapshot, and the newest final review with reviewed_at <= snapshot, using ROW_NUMBER() per profile with a deterministic tie-breaker on the id.
Hint 2
A profile qualifies only if its effective final review is approved AND references the latest version id. A later final rejection overrides an earlier approval, and an approval of an old version does not count.
Hint 3
Aggregate events in their own CTE and LEFT JOIN them so profiles with no events survive; compute country rates as SUM(likes)/SUM(views), not the average of per-profile rates; and drive every filter from fixed parameters, never NOW().
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task is a point-in-time (as-of) snapshot. Every source table is cut off by an explicit parameter so that nothing from after the snapshot leaks in, and so a rerun gives the same answer.
Resolve the time ambiguity first. The qualification cutoff is 2025-09-01 00:00:00, but the event window runs to the end of 09-01. I would keep two parameters:
snapshot_ts = 2025-09-01 00:00:00controls versions and reviews.[win_start, win_end) = [2025-08-26, 2025-09-02)controls events. A half-open interval avoids losing rows at23:59:59.5.
State this assumption explicitly, and note that the job must run after win_end.
Steps
latest_version:ROW_NUMBER()partitioned by profile, ordered bycreated_at DESC, version_id DESC, filtered tocreated_at <= snapshot_ts.final_review: the same pattern over rows withis_final = 1andreviewed_at <= snapshot_ts, ordered byreviewed_at DESC, review_id DESC.qualifying: join both withrn = 1, requireis_deleted = 0,status = 'approved', andfr.version_id = lv.version_id.events_7d: aggregate views and likes per profile in its own CTE, thenLEFT JOIN. Aggregating first avoids fan-out, and the left join keeps zero-event profiles.
1. Snapshot SQL (PostgreSQL)
BEGIN;
DELETE FROM dating_profile_snapshot WHERE snapshot_date = DATE '2025-09-01';
INSERT INTO dating_profile_snapshot
WITH params AS (
SELECT TIMESTAMP '2025-09-01 00:00:00' AS snapshot_ts,
TIMESTAMP '2025-08-26 00:00:00' AS win_start,
TIMESTAMP '2025-09-02 00:00:00' AS win_end
),
latest_version AS (
SELECT pv.profile_id, pv.version_id, pv.bio_text, pv.photos_count,
ROW_NUMBER() OVER (PARTITION BY pv.profile_id
ORDER BY pv.created_at DESC, pv.version_id DESC) rn
FROM profile_versions pv, params p
WHERE pv.created_at <= p.snapshot_ts
),
final_review AS (
SELECT r.profile_id, r.version_id, r.status, r.reviewed_at,
ROW_NUMBER() OVER (PARTITION BY r.profile_id
ORDER BY r.reviewed_at DESC, r.review_id DESC) rn
FROM profile_reviews r, params p
WHERE r.is_final = 1 AND r.reviewed_at <= p.snapshot_ts
),
qualifying AS (
SELECT pr.user_id, u.country, pr.profile_id,
lv.version_id AS latest_version_id,
fr.reviewed_at AS approved_at,
lv.photos_count,
COALESCE(LENGTH(lv.bio_text), 0) AS bio_len
FROM profiles pr
JOIN users u ON u.id = pr.user_id
JOIN latest_version lv ON lv.profile_id = pr.profile_id AND lv.rn = 1
JOIN final_review fr ON fr.profile_id = pr.profile_id AND fr.rn = 1
WHERE pr.is_deleted = 0
AND fr.status = 'approved'
AND fr.version_id = lv.version_id
),
ev AS (
SELECT e.profile_id,
COUNT(*) FILTER (WHERE e.event_type = 'view') AS views,
COUNT(*) FILTER (WHERE e.event_type = 'like') AS likes
FROM profile_events e, params p
WHERE e.event_time >= p.win_start AND e.event_time < p.win_end
GROUP BY e.profile_id
)
SELECT DATE '2025-09-01', q.user_id, q.country, q.profile_id,
q.latest_version_id, q.approved_at, q.photos_count, q.bio_len,
COALESCE(ev.views, 0), COALESCE(ev.likes, 0),
ROUND(COALESCE(ev.likes, 0)::numeric / NULLIF(ev.views, 0), 4)
FROM qualifying q
LEFT JOIN ev ON ev.profile_id = q.profile_id;
COMMIT;
2. Country rollup
SELECT country,
COUNT(*) AS profiles_approved,
AVG(CASE WHEN photos_count >= 1 THEN 1.0 ELSE 0 END) AS pct_with_photo,
AVG(CASE WHEN bio_len >= 10 THEN 1.0 ELSE 0 END) AS "pct_with_bio_10+",
SUM(last_7d_views) AS last_7d_views,
SUM(last_7d_likes) AS last_7d_likes,
SUM(last_7d_likes)::numeric / NULLIF(SUM(last_7d_views), 0) AS last_7d_like_rate
FROM dating_profile_snapshot
WHERE snapshot_date = DATE '2025-09-01'
GROUP BY country;
Use the ratio of sums. Averaging per-profile rates would give a tiny profile with 1 view the same weight as one with 10,000.
3. Validation queries
(a) Final review on a non-latest version (reusing the same CTEs):
SELECT fr.profile_id, fr.version_id AS reviewed_version, lv.version_id AS latest_version
FROM final_review fr
JOIN latest_version lv ON lv.profile_id = fr.profile_id AND lv.rn = 1
WHERE fr.rn = 1 AND fr.version_id <> lv.version_id;
(b) Multiple final reviews per profile:
SELECT profile_id, COUNT(*) AS final_reviews
FROM profile_reviews
WHERE is_final = 1 AND reviewed_at <= TIMESTAMP '2025-09-01 00:00:00'
GROUP BY profile_id
HAVING COUNT(*) > 1;
(c) Late-arriving events. With an ingested_at column, flag events whose business time falls in the window but which landed after the snapshot job ran:
SELECT DISTINCT profile_id
FROM profile_events
WHERE event_time >= TIMESTAMP '2025-08-26' AND event_time < TIMESTAMP '2025-09-02'
AND ingested_at > :snapshot_run_ts;
Without ingested_at, you can only approximate by finding events with event_time > snapshot_ts and comparing the stored snapshot counts to a recomputation. Recommend adding the column.
(d) Idempotency.
- All filters use parameters, never
NOW()orCURRENT_DATE. - The write is
DELETEplusINSERTfor onesnapshot_dateinside one transaction (or a partition overwrite orMERGE). - A check confirms one row per profile:
SELECT profile_id FROM dating_profile_snapshot WHERE snapshot_date = ... GROUP BY 1 HAVING COUNT(*) > 1;should return nothing. - Rerun the job and compare row counts and a checksum such as
SUM(hashtext(profile_id || latest_version_id || last_7d_views || last_7d_likes)). They should match unless late data truly arrived.
Sample walk-through
| Profile | Result | Reason |
|---|---|---|
| 101 | Included | Latest version 1002, final review 5002 approved on 1002 |
| 102 | Excluded | Final review rejected |
| 103 | Excluded | Deleted |
| 104 | Excluded | No final review (the only review is pending and not final) |
Snapshot row: (1, US, 101, 1002, 2025-08-29 12:00:00, 3, 5, 1, 1, 1.0).
Country row: US: profiles_approved=1, pct_with_photo=1.0, pct_with_bio_10+=0.0, views=1, likes=1, rate=1.0.
On this sample, checks (a), (b) and (c) return no rows.
Complexity
Roughly O(V log V + R log R + E) for the window sorts and the event scan. Helpful indexes are (profile_id, created_at) on versions, (profile_id, is_final, reviewed_at) on reviews, and a time-partitioned events table. Space is O(P) for the per-profile aggregates.
Edge cases and common mistakes
- Ties: equal timestamps need an id tie-breaker, or results vary between runs.
- Zero views: use
NULLIFso the rate isNULLrather than a divide-by-zero error. - NULL bio: treat as length 0.
- Joining events before aggregating: this multiplies rows and inflates counts.
- Taking "any approved review": this wrongly includes profiles that were later rejected or edited after approval.
is_deletedis current state, not as-of: true historical correctness needs adeleted_atcolumn or a slowly changing dimension table.- Averaging ratios instead of dividing sums at the country level.