Find Customers with More Than 3 Orders in a Date Window
Quick Overview
Write a SQL query that counts each customer's orders inside a fixed January 2024 date window and returns only customers with more than three. It tests putting a filter in a LEFT JOIN condition, counting a joined column, filtering with HAVING, and producing a deterministic sort order.
Find Customers with More Than 3 Orders in a Date Window
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Given a `Customers` table and an `Orders` table, find every customer who placed more than 3 orders between `2024-01-01` and `2024-01-30`, both dates included. Return each such customer's `customer_id`, `customer_name`, and the number of qualifying orders as `order_count`.
Write the query as a `LEFT JOIN` from `Customers` to `Orders` with the date filter inside the join condition (`order_date >= '2024-01-01'` and `order_date < '2024-01-31'`), then keep only customers whose count is above 3.
```hint Counting after a LEFT JOIN
Think about what a customer with no matching order looks like after the join, and pick the argument to `COUNT` so that customer gets 0.
```
### Input Table(s)
`Customers`
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Customer identifier. Primary key. |
| customer_name | VARCHAR(100) | Customer name. NOT NULL. |
| email | VARCHAR(150) | Email address. May be NULL. |
`Orders`
| Column | Type | Meaning |
|---|---|---|
| order_id | INT | Order identifier. Primary key. |
| customer_id | INT | Customer who placed the order. NOT NULL, foreign key to `Customers.customer_id`. |
| order_date | DATE | Date of the order. NOT NULL. |
| amount | DECIMAL(10,2) | Order amount. May be NULL. |
Keys: `Customers.customer_id` and `Orders.order_id` are primary keys. `Orders.customer_id` references `Customers.customer_id`.
### Output Contract
- Columns, in this order: `customer_id`, `customer_name`, `order_count`.
- `order_count` is an integer: the number of the customer's orders dated 2024-01-01 through 2024-01-30.
- One row per customer, and only customers with `order_count > 3`.
- Sort by `order_count` descending, then `customer_id` ascending. `customer_id` is unique, so the row order is fully determined.
- No rounding is involved.
### Example
`Customers`
| customer_id | customer_name | email |
|---|---|---|
| 1 | Alice | alice@example.com |
| 2 | Bob | NULL |
| 3 | Chen | chen@example.com |
| 4 | Dana | dana@example.com |
| 5 | Eve | eve@example.com |
`Orders`
| order_id | customer_id | order_date | amount |
|---|---|---|---|
| 101 | 1 | 2024-01-01 | 25.00 |
| 102 | 1 | 2024-01-05 | 40.50 |
| 103 | 1 | 2024-01-11 | 12.00 |
| 104 | 1 | 2024-01-30 | 60.00 |
| 105 | 2 | 2023-12-31 | 75.00 |
| 106 | 2 | 2024-01-03 | 20.00 |
| 107 | 2 | 2024-01-15 | 33.25 |
| 108 | 2 | 2024-01-31 | 18.00 |
| 109 | 3 | 2024-01-04 | 15.00 |
| 110 | 3 | 2024-01-08 | NULL |
| 111 | 3 | 2024-01-12 | 42.00 |
| 112 | 3 | 2024-01-19 | 9.99 |
| 113 | 4 | 2024-01-06 | 30.00 |
| 114 | 4 | 2024-01-07 | 22.50 |
| 115 | 4 | 2024-01-14 | 11.00 |
| 116 | 4 | 2024-01-21 | 50.00 |
| 117 | 4 | 2024-01-28 | 27.75 |
Expected output:
| customer_id | customer_name | order_count |
|---|---|---|
| 4 | Dana | 5 |
| 1 | Alice | 4 |
| 3 | Chen | 4 |
Dana has 5 orders in the window. Alice has 4, because orders on 2024-01-01 and 2024-01-30 both count. Chen has 4, and order 110 counts even though its amount is NULL. Bob has 4 orders in total, but orders 105 (2023-12-31) and 108 (2024-01-31) fall outside the window, which leaves him with 2. Eve has no orders. Alice and Chen tie at 4, so customer 1 comes before customer 3.
### Constraints and Clarifications
- `Customers` has at most 10,000 rows and `Orders` at most 100,000 rows.
- Each `order_id` is unique and every `order_date` is a valid `DATE`.
- Every `Orders.customer_id` exists in `Customers`.
- The window includes both 2024-01-01 and 2024-01-30. An order dated 2024-01-31 does not count.
- An order counts even when its `amount` is NULL. A NULL `email` has no effect on the result.
- A customer with no orders in the window has a count of 0 and does not appear in the output.
- Two customers can share a name, so customers are identified by `customer_id`.
- Ties on `order_count` are broken by `customer_id` ascending.
Overview: Write a SQL query that counts each customer's orders inside a fixed January 2024 date window and returns only customers with more than three. It tests putting a filter in a LEFT JOIN condition, counting a joined column, filtering with HAVING, and producing a deterministic sort order.
Given a Customers table and an Orders table, find every customer who placed more than 3 orders between 2024-01-01 and 2024-01-30, both dates included. Return each such customer's customer_id, customer_name, and the number of qualifying orders as order_count.
Write the query as a LEFT JOIN from Customers to Orders with the date filter inside the join condition (order_date >= '2024-01-01' and order_date < '2024-01-31'), then keep only customers whose count is above 3.
Input Table(s)
Customers
Column
Type
Meaning
customer_id
INT
Customer identifier. Primary key.
customer_name
VARCHAR(100)
Customer name. NOT NULL.
email
VARCHAR(150)
Email address. May be NULL.
Orders
Column
Type
Meaning
order_id
INT
Order identifier. Primary key.
customer_id
INT
Customer who placed the order. NOT NULL, foreign key to Customers.customer_id.
order_date
DATE
Date of the order. NOT NULL.
amount
DECIMAL(10,2)
Order amount. May be NULL.
Keys: Customers.customer_id and Orders.order_id are primary keys. Orders.customer_id references Customers.customer_id.
Output Contract
Columns, in this order:
customer_id
,
customer_name
,
order_count
.
order_count
is an integer: the number of the customer's orders dated 2024-01-01 through 2024-01-30.
One row per customer, and only customers with
order_count > 3
.
Sort by
order_count
descending, then
customer_id
ascending.
customer_id
is unique, so the row order is fully determined.
Dana has 5 orders in the window. Alice has 4, because orders on 2024-01-01 and 2024-01-30 both count. Chen has 4, and order 110 counts even though its amount is NULL. Bob has 4 orders in total, but orders 105 (2023-12-31) and 108 (2024-01-31) fall outside the window, which leaves him with 2. Eve has no orders. Alice and Chen tie at 4, so customer 1 comes before customer 3.
Constraints and Clarifications
Customers
has at most 10,000 rows and
Orders
at most 100,000 rows.
Each
order_id
is unique and every
order_date
is a valid
DATE
.
Every
Orders.customer_id
exists in
Customers
.
The window includes both 2024-01-01 and 2024-01-30. An order dated 2024-01-31 does not count.
An order counts even when its
amount
is NULL. A NULL
email
has no effect on the result.
A customer with no orders in the window has a count of 0 and does not appear in the output.
Two customers can share a name, so customers are identified by
customer_id
.
Ties on
order_count
are broken by
customer_id
ascending.