pull/8127/head
Kamran Ahmed 4 weeks ago
parent 2603d5bbf0
commit fff03b1986
  1. 12
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/high-value-orders.md
  2. 97
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/recent-orders.md

@ -39,7 +39,7 @@ setup: |
```
---
The bookstore wants to identify the highest value orders i.e. the orders worth more than or equal to $500. Write a query to find order amounts, dates along with the customer names and emails.
The bookstore wants to identify the highest value orders i.e. the orders worth more than or equal to $500. Write a query to find order amounts, dates along with the customer names.
We have the `customer` and `sale` tables.
@ -69,7 +69,6 @@ We have the `customer` and `sale` tables.
Write a query to find the orders above $500 along with the information of the customers. Your output should have the following columns:
- `customer_name`
- `customer_email`
- `order_amount`
- `order_date`
@ -77,17 +76,16 @@ The output should be ordered by the `order_amount` in descending order.
## Expected Output
| customer_name | customer_email | order_amount | order_date |
| ------------- | ---------------------- | ------------ | ----------------- |
| Jane Smith | jane.smith@example.com | 600.00 | November 11, 2024 |
| John Doe | john.doe@example.com | 550.00 | October 20, 2024 |
| customer_name | order_amount | order_date |
| ------------- | ------------ | ----------------- |
| Jane Smith | 600.00 | November 11, 2024 |
| John Doe | 550.00 | October 20, 2024 |
## Solution
```sql
SELECT
c.name,
c.email,
s.total_amount,
TO_CHAR(s.order_date, 'Month DD, YYYY') AS order_date
FROM customer c

@ -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…
Cancel
Save