parent
2603d5bbf0
commit
fff03b1986
2 changed files with 102 additions and 7 deletions
@ -0,0 +1,97 @@ |
||||
--- |
||||
title: Recent 3 Orders |
||||
description: Practice using LIMIT to get the top N records |
||||
order: 110 |
||||
type: challenge |
||||
setup: | |
||||
```sql |
||||
CREATE TABLE customer ( |
||||
id INT PRIMARY KEY, |
||||
name VARCHAR(255), |
||||
email VARCHAR(255) |
||||
); |
||||
|
||||
CREATE TABLE sale ( |
||||
id INT PRIMARY KEY, |
||||
customer_id INT, |
||||
order_date DATE, |
||||
total_amount DECIMAL(10,2) |
||||
); |
||||
|
||||
INSERT INTO customer (id, name, email) |
||||
VALUES |
||||
(1, 'John Doe', 'john.doe@example.com'), |
||||
(2, 'Jane Smith', 'jane.smith@example.com'), |
||||
(3, 'Alice Johnson', 'alice.johnson@example.com'), |
||||
(4, 'Bob Brown', 'bob.brown@example.com'), |
||||
(5, 'Charlie Davis', 'charlie.davis@example.com'), |
||||
(6, 'David Lee', 'david.lee@example.com'); |
||||
|
||||
INSERT INTO sale (id, customer_id, order_date, total_amount) |
||||
VALUES |
||||
(1, 1, '2024-12-02', 100.00), |
||||
(2, 1, '2024-11-15', 150.00), |
||||
(3, 1, '2024-10-20', 550.00), |
||||
(4, 4, '2024-12-26', 250.00), |
||||
(5, 5, '2024-11-12', 300.00), |
||||
(6, 2, '2024-11-23', 300.00), |
||||
(7, 2, '2024-11-11', 600.00); |
||||
``` |
||||
--- |
||||
|
||||
The bookstore wants to display the 3 most recent orders placed by customers. Write a query to retrieve the customer names, order dates, and total amounts of these orders, ordered by the most recent first. |
||||
|
||||
We have the `customer` and `sale` tables. |
||||
|
||||
> `customer` table has the list of customers. |
||||
|
||||
| id | name | email | |
||||
| --- | ------------- | ------------------------- | |
||||
| 1 | John Doe | john.doe@example.com | |
||||
| 2 | Jane Smith | jane.smith@example.com | |
||||
| 3 | Alice Johnson | alice.johnson@example.com | |
||||
| 4 | Bob Brown | bob.brown@example.com | |
||||
| 5 | Charlie Davis | charlie.davis@example.com | |
||||
| 6 | David Lee | david.lee@example.com | |
||||
|
||||
> `sale` table has the list of sales. |
||||
|
||||
| id | customer_id | order_date | total_amount | |
||||
| --- | ----------- | ---------- | ------------ | |
||||
| 1 | 1 | 2024-12-02 | 100.00 | |
||||
| 2 | 1 | 2024-11-15 | 150.00 | |
||||
| 3 | 1 | 2024-10-20 | 200.00 | |
||||
| 4 | 4 | 2024-12-26 | 250.00 | |
||||
| 5 | 5 | 2024-11-12 | 300.00 | |
||||
| 6 | 2 | 2024-11-23 | 300.00 | |
||||
| 7 | 2 | 2024-11-11 | 300.00 | |
||||
|
||||
Write a query to find the 3 most recent orders along with the information of the customers. Your output should have the following columns: |
||||
|
||||
- `customer_name` |
||||
- `order_date` |
||||
- `order_amount` |
||||
|
||||
The output should be ordered by the `order_date` in descending order. |
||||
|
||||
## Expected Output |
||||
|
||||
| customer_name | order_date | total_amount | |
||||
| ------------- | -------------- | ------------ | |
||||
| Bob Brown | December, 2024 | 250.00 | |
||||
| John Doe | December, 2024 | 100.00 | |
||||
| Jane Smith | November, 2024 | 300.00 | |
||||
|
||||
## Solution |
||||
|
||||
```sql |
||||
SELECT |
||||
c.name, |
||||
TO_CHAR(s.order_date, 'Month, YYYY') AS order_date, |
||||
s.total_amount |
||||
FROM customer c |
||||
JOIN sale s |
||||
ON c.id = s.customer_id |
||||
ORDER BY s.order_date DESC |
||||
LIMIT 3; |
||||
``` |
Loading…
Reference in new issue