sql

Users who viewed then purchased the same item

SQL & Data Modeling · Self Joins · medium · Original Uplift question · PostgreSQL · 12 min

Count the distinct users who VIEWED a product and later PURCHASED that same product (purchase time strictly after the view).

Fixture

events

user_id, product_id, event_type, event_time

create table events (user_id int, product_id int, event_type text, event_time timestamp);
insert into events values
 (1,10,'view','2026-01-01 10:00'),(1,10,'purchase','2026-01-01 10:30'),
 (2,20,'view','2026-01-01 09:00'),(2,20,'view','2026-01-01 09:05'),
 (3,30,'purchase','2026-01-01 08:00'),(3,30,'view','2026-01-01 09:00');

Evaluation criteria

  • Self-joins views to purchases on user_id and product_id
  • Requires purchase time strictly after view time
  • Filters event_type correctly on each side
  • Counts DISTINCT users

Detailed grading feedback appears after you submit.

Source

Original Uplift question based on a common interview pattern.

Your answer

You get one graded attempt here without an account. Create a free account for unlimited attempts, saved progress and community write-ups.

Answers stay hidden until you ask. Reveal the full model write-up when you are ready to compare.

Community

How other people solved this

There is rarely one right query or one right framing. Read how others approached it, and share your own once you have submitted an answer.

Reading is open to everyone. Sign in to share your approach or mark a write-up as helpful.

Loading write-ups…