Update inactive customer query

pull/8127/head
Kamran Ahmed 4 weeks ago
parent 6e3cb6bde0
commit 468cc2b3d2
  1. 37
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/multi-section-authors.md
  2. 87
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/specific-book-customers.md

@ -37,9 +37,9 @@ setup: |
```
---
Given the following two tables `fiction_book` and `non_fiction_book` with the following data:
The bookstore is organizing a special "Genre-Crossing Authors" showcase to highlight versatile writers who have published both fiction and non-fiction works. The marketing team believes these authors might attract readers interested in exploring different genres. Your task is to help identify authors who have books in both sections of the store.
> `fiction_book` table has the following data
Given the following data in table `fiction_book`
| id | title | author | price |
| --- | ----------------------- | ------------------- | ----- |
@ -49,7 +49,7 @@ Given the following two tables `fiction_book` and `non_fiction_book` with the fo
| 4 | The Hobbit | J.R.R. Tolkien | 29.99 |
| 5 | The Doors of Perception | Aldous Huxley | 12.99 |
> `non_fiction_book` table looks like this:
And the following data in table `non_fiction_book`
| id | title | author | price |
| --- | ---------------------------------- | ----------------- | ----- |
@ -59,14 +59,14 @@ Given the following two tables `fiction_book` and `non_fiction_book` with the fo
| 4 | Pride and Prejudice: A Study Guide | John Smith | 12.99 |
| 5 | The Doors of Perception | Aldous Huxley | 12.99 |
Write a query to find the authors who have written books in both `fiction_book` and `non_fiction_book` tables.
Write a query to identify authors who have published works in both fiction and non-fiction categories. The marketing team wants to see:
## Expected Results
- The author's name
Your output should only contain the author names:
## Expected Output
| author |
| ------------------- |
| ------------- |
| Aldous Huxley |
## Solution
@ -78,3 +78,26 @@ INTERSECT
SELECT author
FROM non_fiction_book;
```
### Explanation
Let's break down how this query works:
First, we get all authors from the fiction books table:
```sql
SELECT author
FROM fiction_book
```
Then we use `INTERSECT` to find authors that also appear in the non-fiction books table:
```sql
INTERSECT
SELECT author
FROM non_fiction_book
```
The `INTERSECT` operator returns only the values that appear in both queries. This effectively finds authors who have written both fiction and non-fiction books.
This query helps the marketing team identify versatile authors who have crossed genres, allowing them to create targeted promotions and displays that showcase the diverse works of these authors.

@ -54,9 +54,9 @@ setup: |
```
---
We have following three tables `customer`, `sale`, and `book`. You are required to write a query to find all customers who have purchased any book by "Harper Lee".
The bookstore is planning a special author event featuring "Harper Lee". To make the event a success, they want to invite customers who have previously purchased her books, believing these readers would be most interested in attending. Your task is to help identify these customers from the database.
> `customer` table has the list of customers.
Given the following data in table `customer`
| id | name | email |
| --- | ------------- | ------------------------- |
@ -67,19 +67,19 @@ We have following three tables `customer`, `sale`, and `book`. You are required
| 5 | Charlie Davis | charlie.davis@example.com |
| 6 | David Lee | david.lee@example.com |
> `sale` table has the list of sales made to customers.
And the following data in table `sale`
| 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 |
| id | customer_id | book_id | order_date | total_amount |
| --- | ----------- | ------- | ---------- | ------------ |
| 1 | 1 | 1 | 2024-12-02 | 100.00 |
| 2 | 1 | 2 | 2024-11-15 | 150.00 |
| 3 | 1 | 3 | 2024-10-20 | 200.00 |
| 4 | 4 | 4 | 2024-12-26 | 250.00 |
| 5 | 5 | 5 | 2024-11-12 | 300.00 |
| 6 | 2 | 1 | 2024-11-23 | 300.00 |
| 7 | 2 | 2 | 2024-11-11 | 300.00 |
> `book` table has the list of books.
And the following data in table `book`
| id | title | author |
| --- | ---------------------- | ------------------- |
@ -89,9 +89,14 @@ We have following three tables `customer`, `sale`, and `book`. You are required
| 4 | Pride and Prejudice | Jane Austen |
| 5 | The Catcher in the Rye | J.D. Salinger |
## Expected Output
Write a query to identify all customers who have purchased books by Harper Lee. The events team wants to see:
- The customer's ID
- The customer's name
- Which Harper Lee book they purchased
- When they made the purchase (formatted as "Month DD, YYYY")
The expected output should be a list of customers who have purchased any book by "Harper Lee" with their id, name, title of the book they purchased and the date of the purchase.
## Expected Output
| id | name | title | purchase_date |
| --- | ---------- | --------------------- | ----------------- |
@ -104,15 +109,47 @@ The expected output should be a list of customers who have purchased any book by
SELECT
c.id,
c.name,
c.email,
b.title,
TO_CHAR(s.order_date, 'Month, DD, YYYY') purchase_date
FROM
customer c
INNER JOIN
sale s on c.id = s.customer_id
INNER JOIN
book b ON s.book_id = b.id
WHERE
b.author = 'Harper Lee'
TO_CHAR(s.order_date, 'Month DD, YYYY') AS purchase_date
FROM customer c
INNER JOIN sale s
ON c.id = s.customer_id
INNER JOIN book b
ON s.book_id = b.id
WHERE b.author = 'Harper Lee'
ORDER BY s.order_date DESC;
```
### Explanation
Let's break down how this query works:
We start by joining all three tables to connect customers with their purchases and the books they bought:
```sql
FROM customer c
INNER JOIN sale s
ON c.id = s.customer_id
INNER JOIN book b
ON s.book_id = b.id
```
We filter for only Harper Lee's books:
```sql
WHERE b.author = 'Harper Lee'
```
We format the date to be more readable:
```sql
TO_CHAR(s.order_date, 'Month DD, YYYY') AS purchase_date
```
Finally, we order by purchase date to see the most recent purchases first:
```sql
ORDER BY s.order_date DESC
```
This query helps the events team identify customers who have shown interest in Harper Lee's work, allowing them to create a targeted invitation list for the author event.

Loading…
Cancel
Save