Practice Free
HardCTEs High Frequency

Average Days Elapsed Between First and Second Order

Frequently Asked By:AmazonShopifyUber
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

Write a query using CTEs to compute the repeat purchase latency for each customer who made at least 2 orders: 1. In a CTE `ranked_orders`, use `ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date ASC)` to rank orders chronologically. 2. In a second CTE `paired_orders`, join order 1 to order 2 for each customer and calculate the day difference (`o2.order_date - o1.order_date`). Return: 1. `customer_id` 2. `first_order_date` 3. `second_order_date` 4. `days_to_second_order` Order results by `days_to_second_order` ascending, then by `customer_id` ascending.

Why Technical Interviewers Ask This

E-commerce retention marketers measure the velocity of repeat buyer conversion by calculating the exact calendar days between customer Order #1 and Order #2.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass ctes constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
WITH ranked_orders AS (
        SELECT 
          order_id,
          customer_id,
          order_date,
          ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date ASC) AS order_num
        FROM customer_orders
      )
      SELECT 
        o1.customer_id,
        o1.order_date AS first_order_date,
        o2.order_date AS second_order_date,
        (o2.order_date - o1.order_date) AS days_to_second_order
      FROM ranked_orders o1
      JOIN ranked_orders o2 ON o1.customer_id = o2.customer_id AND o1.order_num = 1 AND o2.order_num = 2
      ORDER BY days_to_second_order ASC, o1.customer_id ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in CTEs. It meets the target criteria: Customer latency in days between first and second e-commerce purchases.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Amazon and Shopify interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable CTEs queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions