Update inactive customer query

feat/course
Kamran Ahmed 2 weeks ago
parent 871e195e47
commit 6e3cb6bde0
  1. 59
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/high-value-orders.md

@ -39,9 +39,9 @@ setup: |
``` ```
--- ---
Given the following three tables you are required to write a query to find the orders worth more than or equal to $500. The bookstore wants to identify their most valuable orders to better understand their high-spending customers. The marketing team plans to create a VIP program for customers who make large purchases, and they've defined a high-value order as any purchase of $500 or more. They need a report of all such orders to begin their analysis.
> `customer` table has the list of customers. Given the following data in table `customer`
| id | name | email | | id | name | email |
| --- | ------------- | ------------------------- | | --- | ------------- | ------------------------- |
@ -52,25 +52,25 @@ Given the following three tables you are required to write a query to find the o
| 5 | Charlie Davis | charlie.davis@example.com | | 5 | Charlie Davis | charlie.davis@example.com |
| 6 | David Lee | david.lee@example.com | | 6 | David Lee | david.lee@example.com |
> `sale` table has the list of sales. And the following data in table `sale`
| id | customer_id | order_date | total_amount | | id | customer_id | order_date | total_amount |
| --- | ----------- | ---------- | ------------ | | --- | ----------- | ---------- | ------------ |
| 1 | 1 | 2024-12-02 | 100.00 | | 1 | 1 | 2024-12-02 | 100.00 |
| 2 | 1 | 2024-11-15 | 150.00 | | 2 | 1 | 2024-11-15 | 150.00 |
| 3 | 1 | 2024-10-20 | 200.00 | | 3 | 1 | 2024-10-20 | 550.00 |
| 4 | 4 | 2024-12-26 | 250.00 | | 4 | 4 | 2024-12-26 | 250.00 |
| 5 | 5 | 2024-11-12 | 300.00 | | 5 | 5 | 2024-11-12 | 300.00 |
| 6 | 2 | 2024-11-23 | 300.00 | | 6 | 2 | 2024-11-23 | 300.00 |
| 7 | 2 | 2024-11-11 | 300.00 | | 7 | 2 | 2024-11-11 | 600.00 |
Your output should contain the orders with amount greater than or equal to `$500` and the following columns for each order: Write a query to identify all orders worth $500 or more. The marketing team wants to see:
- `customer_name` - The customer's name
- `order_amount` - The order amount
- `order_date` - When the order was placed (formatted as "Month DD, YYYY")
Results should be sorted by `order_amount` in descending order. Results should be ordered by amount from highest to lowest to easily identify the most valuable orders.
## Expected Output ## Expected Output
@ -83,11 +83,44 @@ Results should be sorted by `order_amount` in descending order.
```sql ```sql
SELECT SELECT
c.name, c.name AS customer_name,
s.total_amount, s.total_amount AS order_amount,
TO_CHAR(s.order_date, 'Month DD, YYYY') AS order_date TO_CHAR(s.order_date, 'Month DD, YYYY') AS order_date
FROM customer c FROM customer c
JOIN sale s ON c.id = s.customer_id JOIN sale s
ON c.id = s.customer_id
WHERE s.total_amount >= 500 WHERE s.total_amount >= 500
ORDER BY s.total_amount DESC; ORDER BY s.total_amount DESC;
``` ```
### Explanation
Let's break down how this query works:
We join the `customer` and `sale` tables to get customer information with their orders:
```sql
FROM customer c
JOIN sale s
ON c.id = s.customer_id
```
We filter for high-value orders using the WHERE clause:
```sql
WHERE s.total_amount >= 500
```
We format the date to be more readable using `TO_CHAR`:
```sql
TO_CHAR(s.order_date, 'Month DD, YYYY') AS order_date
```
Finally, we order the results by total amount in descending order to see the highest value orders first:
```sql
ORDER BY s.total_amount DESC
```
This query helps the marketing team identify their highest-value orders and the customers who placed them, providing valuable information for their VIP program initiative.

Loading…
Cancel
Save