Build a Customer RFM Feature Table for ML with SQL

Quick Overview

Write a SQL query that builds one feature row per ordering customer for a machine learning model: recency, frequency, total and average spend, tenure and total sessions as of a fixed date. It tests aggregating across two child tables without double counting, date arithmetic and NULL handling.

Build a Customer RFM Feature Table for ML with SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Build a per-customer feature table for a machine learning model, using `2024-06-30` as the reference date. Each row describes one customer's purchase recency, order frequency, spend, tenure and engagement. Customers who have never placed an order are left out. ```hint Count the joined rows first Before writing any aggregate, work out how many rows a join produces for a customer with two orders and one engagement row, and what that does to each sum and count. ``` ### Input Table(s) `customers` | Column | Type | Meaning | |---|---|---| | customer_id | INT | Customer identifier | | signup_date | DATE | Date the customer signed up (never `NULL`) | Key: `customer_id` is the primary key. `orders` | Column | Type | Meaning | |---|---|---| | order_id | INT | Order identifier | | customer_id | INT | Customer who placed the order (never `NULL`) | | order_date | DATE | Date of the order (never `NULL`) | | amount | DECIMAL(10,2) | Order amount (never `NULL`) | Key: `order_id` is the primary key. `engagement` | Column | Type | Meaning | |---|---|---| | engagement_id | INT | Engagement record identifier | | customer_id | INT | Customer the record belongs to (never `NULL`) | | session_count | INT | Number of sessions in this record; may be `NULL` | Key: `engagement_id` is the primary key. A customer can have zero, one or many engagement rows. ### Output Contract Return these columns in this order, one row per customer who has at least one order: | Column | Rule | |---|---| | customer_id | The customer identifier | | recency_days | Whole days from the customer's latest `order_date` to `2024-06-30` | | frequency | Number of orders the customer placed | | monetary_total | Sum of `amount` over the customer's orders, rounded to 2 decimals | | monetary_avg | Average `amount` per order, rounded to 2 decimals | | tenure_days | Whole days from `signup_date` to `2024-06-30` | | total_sessions | Sum of `session_count` over all of the customer's engagement rows; `0` if there are no rows or every value is `NULL` | Sort by `customer_id` ascending. ### Example `customers` | customer_id | signup_date | |---|---| | 1 | 2024-01-01 | | 2 | 2024-03-15 | | 3 | 2024-05-01 | `orders` | order_id | customer_id | order_date | amount | |---|---|---|---| | 10 | 1 | 2024-06-01 | 100.00 | | 11 | 1 | 2024-06-20 | 50.50 | | 12 | 2 | 2024-04-10 | 200.00 | `engagement` | engagement_id | customer_id | session_count | |---|---|---| | 100 | 1 | 12 | | 101 | 2 | 5 | Expected result: | customer_id | recency_days | frequency | monetary_total | monetary_avg | tenure_days | total_sessions | |---|---|---|---|---|---|---| | 1 | 10 | 2 | 150.50 | 75.25 | 181 | 12 | | 2 | 81 | 1 | 200.00 | 200.00 | 107 | 5 | Customer 3 has no orders, so it is excluded. Customer 1 has two orders totaling 150.50 (average 75.25), the latest on 2024-06-20, which is 10 days before the reference date; signing up on 2024-01-01 gives 181 days of tenure (2024 is a leap year), and the single engagement row contributes 12 sessions. Customer 2 has one order of 200.00 on 2024-04-10 (81 days before), signed up 107 days before, and has 5 sessions. ### Constraints and Clarifications - There are 1 to 10,000 customers. - `amount` is between 0 and 1,000,000. - `session_count` may be `NULL`. A customer may have no engagement rows or several; `total_sessions` counts each engagement row once, however many orders the customer has. - All dates are on or before `2024-06-30`, so `recency_days` and `tenure_days` are never negative. - Orders or engagement rows whose `customer_id` is not in `customers` are ignored.

Overview: Write a SQL query that builds one feature row per ordering customer for a machine learning model: recency, frequency, total and average spend, tenure and total sessions as of a fixed date. It tests aggregating across two child tables without double counting, date arithmetic and NULL handling.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Build a per-customer feature table for a machine learning model, using 2024-06-30 as the reference date. Each row describes one customer's purchase recency, order frequency, spend, tenure and engagement. Customers who have never placed an order are left out.

Input Table(s)

customers

ColumnTypeMeaning
customer_idINTCustomer identifier
signup_dateDATEDate the customer signed up (never NULL)

Key: customer_id is the primary key.

orders

ColumnTypeMeaning
order_idINTOrder identifier
customer_idINTCustomer who placed the order (never NULL)
order_dateDATEDate of the order (never NULL)
amountDECIMAL(10,2)Order amount (never NULL)

Key: order_id is the primary key.

engagement

ColumnTypeMeaning
engagement_idINTEngagement record identifier
customer_idINTCustomer the record belongs to (never NULL)
session_countINTNumber of sessions in this record; may be NULL

Key: engagement_id is the primary key. A customer can have zero, one or many engagement rows.

Output Contract

Return these columns in this order, one row per customer who has at least one order:

ColumnRule
customer_idThe customer identifier
recency_daysWhole days from the customer's latest order_date to 2024-06-30
frequencyNumber of orders the customer placed
monetary_totalSum of amount over the customer's orders, rounded to 2 decimals
monetary_avgAverage amount per order, rounded to 2 decimals
tenure_daysWhole days from signup_date to 2024-06-30
total_sessionsSum of session_count over all of the customer's engagement rows; 0 if there are no rows or every value is NULL

Sort by customer_id ascending.

Example

customers

customer_idsignup_date
12024-01-01
22024-03-15
32024-05-01

orders

order_idcustomer_idorder_dateamount
1012024-06-01100.00
1112024-06-2050.50
1222024-04-10200.00

engagement

engagement_idcustomer_idsession_count
100112
10125

Expected result:

customer_idrecency_daysfrequencymonetary_totalmonetary_avgtenure_daystotal_sessions
1102150.5075.2518112
2811200.00200.001075

Customer 3 has no orders, so it is excluded. Customer 1 has two orders totaling 150.50 (average 75.25), the latest on 2024-06-20, which is 10 days before the reference date; signing up on 2024-01-01 gives 181 days of tenure (2024 is a leap year), and the single engagement row contributes 12 sessions. Customer 2 has one order of 200.00 on 2024-04-10 (81 days before), signed up 107 days before, and has 5 sessions.

Constraints and Clarifications

  • There are 1 to 10,000 customers.
  • amount is between 0 and 1,000,000.
  • session_count may be NULL . A customer may have no engagement rows or several; total_sessions counts each engagement row once, however many orders the customer has.
  • All dates are on or before 2024-06-30 , so recency_days and tenure_days are never negative.
  • Orders or engagement rows whose customer_id is not in customers are ignored.