From 8d1cc145e53091fe444ab7e09a971255045df074 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 2 Jan 2025 13:34:06 +0000 Subject: [PATCH] Update inactive customer query --- .../lessons/inactive-customers.md | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/inactive-customers.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/inactive-customers.md index 58481e365..2fa048213 100644 --- a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/inactive-customers.md +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/inactive-customers.md @@ -39,11 +39,9 @@ setup: | ``` --- -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. -We have the `customer` and `sale` tables. - -> `customer` table has the list of customers. +Given the following data in table `customer` | id | name | email | | --- | ------------- | ------------------------- | @@ -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.