Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Conversion Rate Change with CTEs

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Conversion Rate Change with CTEs. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Context

Capgemini Government Solutions wants to evaluate whether product changes improve user conversion behavior. Each product change should be compared with activity during the 30 days immediately before and after the change.

Task

Write a PostgreSQL query using CTEs and joins to compare conversion rates for every valid product change.

Requirements

  1. Classify each visit as before or after based on a 30-day window around change_date.
  2. Use a LEFT JOIN so products with no visits remain in the result.
  3. Calculate visits, converted visits, and conversion rates for both periods. Use NULL for a rate when the period has no visits.
  4. Return the conversion-rate change as after_rate - before_rate, ordered by product_id.

Schema

product_changes
ColumnTypeDescription
change_idPKINTUnique product change identifier
product_idINTProduct associated with the change
change_dateDATEDate the product change took effect
change_labelVARCHAR(100)Short description of the change
product_visits
ColumnTypeDescription
visit_idPKINTUnique product visit identifier
product_idINTProduct viewed during the visit
visitor_idINTAnonymous or authenticated visitor identifier
visited_atDATEDate of the product visit
conversions
ColumnTypeDescription
conversion_idPKINTUnique conversion identifier
visit_idINTVisit that produced the conversion
converted_atDATEDate of conversion
Tablesproduct_changesproduct_visitsconversions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results