Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Unify CRM and Google Analytics View

HardSQL · PostgreSQL00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the SQL screen.

The question is on your right: Unify CRM and Google Analytics View. Read through the requirements and the four tables first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

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

Problem

Business Context

You’re a data engineer at a fast-growing e-commerce retailer (~3M monthly active users) that sells direct-to-consumer. Marketing spends heavily on paid search and email, but the company can’t reliably connect anonymous web behavior (Google Analytics-style events) to known customers (CRM) once they purchase or log in. This leads to mis-attributed CAC/LTV, broken retargeting audiences, and inconsistent executive reporting.

Your warehouse receives:

  • CRM data with customer records and orders.
  • Web analytics events (GA-like) keyed by ga_client_id (cookie/device identifier).
  • A crosswalk table mapping ga_client_id to customer_id when a user logs in or completes checkout.

Because users can browse anonymously on multiple devices, a single customer may map to multiple ga_client_ids over time. You want a single customer view that rolls up web behavior and revenue.

Task

Write a SQL query that produces a unified customer-level dataset for January 2025.

Requirements

  1. Output one row per customer_id.
  2. Include:
    • customer_id, email
    • first_touch_channel: the channel of the customer’s earliest known web event (across all mapped GA client IDs)
    • sessions_jan_2025: distinct session count in Jan 2025
    • pageviews_jan_2025: total page_view events in Jan 2025
    • orders_jan_2025: number of orders placed in Jan 2025
    • revenue_jan_2025: sum of order totals in Jan 2025
  3. Only count web events that can be attributed to a customer via the mapping table.
  4. If a customer has no January web events, they should still appear if they have a January order (and web metrics should be 0 / NULL as appropriate).
  5. Use a deterministic rule for first touch: earliest event_ts; if ties, choose lexicographically smallest channel.

Schema

crm_customers
ColumnTypeDescription
customer_idPKINTCRM customer primary key
emailVARCHAR(255)Customer email address
created_atTIMESTAMPTimestamp when the customer record was created
crm_orders
ColumnTypeDescription
order_idPKBIGINTOrder primary key
customer_idINTCustomer placing the order (FK to crm_customers)
order_tsTIMESTAMPOrder timestamp
order_totalDECIMAL(10,2)Total charged amount for the order
ga_events
ColumnTypeDescription
event_idPKBIGINTEvent primary key
ga_client_idVARCHAR(64)GA client/cookie identifier
session_idVARCHAR(64)Session identifier
event_tsTIMESTAMPEvent timestamp
event_nameVARCHAR(50)Event type (e.g., page_view)
channelVARCHAR(50)Marketing channel attribution for the event
identity_map
ColumnTypeDescription
ga_client_idPKVARCHAR(64)GA client identifier (one side of the crosswalk)
customer_idINTMapped CRM customer_id
first_seen_tsTIMESTAMPFirst time this mapping was observed
Tablescrm_customerscrm_ordersga_eventsidentity_map
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results