36. Most Recent Three Orders for Each Customer

We have two tables: Customers and Orders.

Table: Customers

╔═════════════╦═════════╗
ā•‘ Column Name ā•‘  Type   ā•‘
╠═════════════╬═════════╣
ā•‘ customer_id ā•‘   int   ā•‘
║─────────────┼─────────║
ā•‘    name     ā•‘ varchar ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • customer_id is the primary key for this table. It contains information about the customers.

Table: Orders

╔═════════════╦════════╗
ā•‘ Column Name ā•‘  Type  ā•‘
╠═════════════╬════════╣
ā•‘  order_id   ā•‘  int   ā•‘
║─────────────┼────────║
ā•‘ order_date  ā•‘  date  ā•‘
║─────────────┼────────║
ā•‘ customer_id ā•‘  int   ā•‘
║─────────────┼────────║
ā•‘    cost     ā•‘  int   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • order_id is the primary key for this table. It contains the orders made by customers.
  • customer_id references the customer who placed the order.
  • Each customer has only one order per day.

Write a solution to find the most recent three orders for each customer. If a customer has less than three orders, return all of their orders.

Return the result table ordered by customer_name in ascending order. In case of a tie, order by customer_id in ascending order. If there is still a tie, order them by order_date in descending order.

Example 1:

Example

Input:

Customers table:

╔═════════════╦═══════════╗
ā•‘ customer_id ā•‘   name    ā•‘
╠═════════════╬═══════════╣
ā•‘      1      ā•‘  Winston  ā•‘
║─────────────┼───────────║
ā•‘      2      ā•‘ Jonathan  ā•‘
║─────────────┼───────────║
ā•‘      3      ā•‘ Annabelle ā•‘
║─────────────┼───────────║
ā•‘      4      ā•‘  Marwan   ā•‘
║─────────────┼───────────║
ā•‘      5      ā•‘  Khaled   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Orders table:

╔══════════╦════════════╦═════════════╦══════╗
ā•‘ order_id ā•‘ order_date ā•‘ customer_id ā•‘ cost ā•‘
╠══════════╬════════════╬═════════════╬══════╣
ā•‘    1     ā•‘ 2020-07-31 ā•‘      1      ā•‘  30  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    2     ā•‘ 2020-07-30 ā•‘      2      ā•‘  40  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    3     ā•‘ 2020-07-31 ā•‘      3      ā•‘  70  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    4     ā•‘ 2020-07-29 ā•‘      4      ā•‘ 100  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    5     ā•‘ 2020-06-10 ā•‘      1      ā•‘ 1010 ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    6     ā•‘ 2020-08-01 ā•‘      2      ā•‘ 102  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    7     ā•‘ 2020-08-01 ā•‘      3      ā•‘ 111  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    8     ā•‘ 2020-08-03 ā•‘      1      ā•‘  99  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘    9     ā•‘ 2020-08-07 ā•‘      2      ā•‘  32  ā•‘
║──────────┼────────────┼─────────────┼──────║
ā•‘   10     ā•‘ 2020-07-15 ā•‘      1      ā•‘  2   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•

Output:

╔═══════════════╦═════════════╦══════════╦════════════╗
ā•‘ customer_name ā•‘ customer_id ā•‘ order_id ā•‘ order_date ā•‘
╠═══════════════╬═════════════╬══════════╬════════════╣
ā•‘   Annabelle   ā•‘      3      ā•‘    7     ā•‘ 2020-08-01 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘   Annabelle   ā•‘      3      ā•‘    3     ā•‘ 2020-07-31 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘   Jonathan    ā•‘      2      ā•‘    9     ā•‘ 2020-08-07 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘   Jonathan    ā•‘      2      ā•‘    6     ā•‘ 2020-08-01 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘   Jonathan    ā•‘      2      ā•‘    2     ā•‘ 2020-07-30 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘    Marwan     ā•‘      4      ā•‘    4     ā•‘ 2020-07-29 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘    Winston    ā•‘      1      ā•‘    8     ā•‘ 2020-08-03 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘    Winston    ā•‘      1      ā•‘    1     ā•‘ 2020-07-31 ā•‘
║───────────────┼─────────────┼──────────┼────────────║
ā•‘    Winston    ā•‘      1      ā•‘   10     ā•‘ 2020-07-15 ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

Winston has 4 orders, so we discard the oldest order from 2020-06-10.

Annabelle only has 2 orders, so we return them.

Jonathan has exactly 3 orders.

Marwan has only one order.

We sort the result by customer_name (ascending), then by customer_id (ascending), and then by order_date (descending).

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Customers
Orders