Lessons › SQL › Go further in SQL: Business questions
SQL lesson
What was each customer's first order?
What was each customer's first order? MIN(order_date) gives the date but not the order. Number each customer's orders by date instead, starting again for every customer, and keep number 1.
Try this lesson free →Example
Number each group's rows, keep number 1
WITH numbered AS (
SELECT customer_id,
order_id,
order_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS rn
FROM orders
)
SELECT customer_id, order_id, order_date
FROM numbered
WHERE rn = 1| customer_id | order_id | order_date |
|---|---|---|
| C101 | 1 | 2026-02-03 |
| C102 | 2 | 2026-02-05 |
| C103 | 4 | 2026-02-14 |
| C104 | 5 | 2026-02-20 |
| C105 | 7 | 2026-03-04 |
| C106 | 11 | 2026-02-22 |
Step by step
- Their earliest order.
WITH numbered AS (SELECT customer_id, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, order_id, order_date FROM numbered WHERE rn = 1 - No join.
WITH numbered AS (SELECT customer_id, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, order_id, order_date FROM numbered WHERE rn = 1 - WHERE rn = 1.
WITH numbered AS (SELECT customer_id, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, order_id, order_date FROM numbered WHERE rn = 1 - ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date).
WITH numbered AS (SELECT customer_id, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, order_id, order_date FROM numbered WHERE rn = 1
Practice
1. What was each customer's first order? Show customer_id, order_id and order_date.
WITH numbered AS (SELECT customer_id, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, order_id, order_date FROM numbered WHERE rn = 12. What was each customer's latest order? Show customer_id, order_id and order_date.
WITH numbered AS (SELECT customer_id, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn FROM orders) SELECT customer_id, order_id, order_date FROM numbered WHERE rn = 13. What was each store's first order? Show store, order_id and order_date.
WITH numbered AS (SELECT store, order_id, order_date, ROW_NUMBER() OVER (PARTITION BY store ORDER BY order_date) AS rn FROM orders) SELECT store, order_id, order_date FROM numbered WHERE rn = 14. What did each customer buy first? Show customer_id and product.
WITH numbered AS (SELECT customer_id, product, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, product FROM numbered WHERE rn = 15. Which customers used a coupon on their first order? Show customer_id and coupon.
WITH numbered AS (SELECT customer_id, coupon, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders) SELECT customer_id, coupon FROM numbered WHERE rn = 1 AND coupon IS NOT NULLLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →