Menu

SQL Challenge: Can You Find the Second Purchase of Every Customer Using ROW_NUMBER()?
📰

SQL Challenge: Can You Find the Second Purchase of Every Customer Using ROW_NUMBER()?

DEV Community: database·Kailas Warade·18 days ago
#TgyBlJFU
Long article (423 words) — no AI summary yet.
Reading 0:00
15s threshold

In real-world data analysis, businesses often need to know when customers make their second purchase. This helps them understand buying habits, track repeat customers and study customer activity.

You have an "orders" table with order IDs, customer IDs and order dates.

Your task is to write an SQL query to find the second purchase made by each customer using the ROW_NUMBER() window function.

Your Challenge

Write an SQL query to:

Display customer ID, order ID and order date.

Use ROW_NUMBER() to number each customer's orders.

Use PARTITION BY to start the numbering again for every customer.

Arrange orders by date, from oldest to newest.

Return only the second order of each customer.

Get the result without changing the original orders table.

orders

order_id|customer_id |order_date

1001 | C101 | 05-Jan-2026

1002 | C101 | 12-Jan-2026

1003 | C101 | 20-Jan-2026

2001 | C102 | 03-Feb-2026

2002 | C102 | 10-Feb-2026

3001 | C103

| 15-Feb-2026

Can you write the query before checking the solution?

The…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.