Given the following data in `customer` and `sale` tables, we need to find the customers who have never placed an order.
The bookstore's marketing team wants to run a special promotion targeting customers who haven't made any purchases yet. They believe these potential customers might need a little extra encouragement to make their first purchase. Your task is to help identify these inactive customers from the database.
@ -54,7 +52,7 @@ We have the `customer` and `sale` tables.
| 5 | Charlie Davis | charlie.davis@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 |
| --- | ----------- | ---------- | ------------ |
@ -66,7 +64,7 @@ We have the `customer` and `sale` tables.
| 6 | 2 | 2024-11-23 | 300.00 |
| 7 | 2 | 2024-11-11 | 300.00 |
Write a query to find customers who have never placed an order.
Write a query to identify all customers who have never placed an order. These are the customers the marketing team wants to target with their special promotion.
## Expected Output
@ -84,3 +82,25 @@ LEFT JOIN sale s
ON c.id = s.customer_id
WHERE s.id IS NULL;
```
### Explanation
We start with the `customer` table as we want to keep all customers regardless of whether they have made a purchase:
```sql
FROM customer c
```
We use a `LEFT JOIN` with the `sale` table to include all customers and their sales (if any):
```sql
LEFT JOIN sale s ON c.id = s.customer_id
```
The `WHERE` clause filters for customers who have no matching records in the `sale` table:
```sql
WHERE s.id IS NULL
```
This query efficiently identifies customers who haven't made any purchases yet. The LEFT JOIN ensures we keep all customers from the customer table, while the `WHERE` clause filters out those who have made purchases.