easy

Most Recent Order per Customer

You are working as a Data Engineer for an e-commerce platform. The business team wants to analyze customer recency by identifying the latest purchase made by each customer.

You are given the following table:

orders

  • order_id (INT) – Unique identifier for each order

  • customer_id (INT) – Identifier for the customer

  • order_date (DATE) – Date when the order was placed

  • order_amount (NUMERIC) – Total amount of the order

Task:

Write a SQL query to return the most recent order date for each customer.

Expected Output:

  • customer_id

  • most_recent_order_date

The result should have one row per customer.

1
2
3
4
5
Preparing...
Most Recent Order per Customer

You are working as a Data Engineer for an e-commerce platform. The business team wants to analyze customer recency by identifying the latest purchase made by each customer. You are given the following table: orders order_id (INT) – Unique identifier for each order customer_id (INT) – Identifier for the customer order_date (DATE) – Date when the order was placed order_amount (NUMERIC) – Total amount of the order Task: Write a SQL query to return the most recent order date for each customer . Expected Output: customer_id most_recent_order_date The result should have one row per customer.

Table Setup (SQL Schema)

-- DDL: Create table
CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    order_amount NUMERIC
);

-- DML: Insert sample data
INSERT INTO orders (order_id, customer_id, order_date, order_amount) VALUES
(1, 201, '2024-01-05', 120.00),
(2, 202, '2024-01-10', 75.50),
(3, 201, '2024-02-15', 200.00),
(4, 203, '2024-01-20', 50.00),
(5, 202, '2024-03-01', 180.00),
(6, 201, '2024-03-05', 95.00),
(7, 204, '2024-02-25', 300.00),
(8, 203, '2024-02-10', 80.00),
(9, 204, '2024-03-10', 220.00),
(10, 205, '2024-03-12', 60.00);
Home Videos Quiz Blog