From 5e98ae2d29af48c666d812ac95fbc2ae41214063 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 2 Jan 2025 04:34:24 +0000 Subject: [PATCH] Add new challenges --- .../lessons/expensive-books.md | 2 +- .../lessons/high-value-orders.md | 10 +- .../lessons/inactive-customers.md | 2 +- .../lessons/multi-section-authors.md | 2 +- .../lessons/recent-orders.md | 6 +- .../lessons/referred-customers.md | 66 ++++++++++ .../lessons/specific-book-customers.md | 118 ++++++++++++++++++ .../lessons/trending-tech-books.md | 4 +- 8 files changed, 195 insertions(+), 15 deletions(-) create mode 100644 src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/referred-customers.md create mode 100644 src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/specific-book-customers.md diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/expensive-books.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/expensive-books.md index 1e8c36f30..ef94e502a 100644 --- a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/expensive-books.md +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/expensive-books.md @@ -1,7 +1,7 @@ --- title: Expensive Books description: Practice creating a report with multiple tables -order: 110 +order: 190 type: challenge setup: | ```sql diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/high-value-orders.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/high-value-orders.md index 155c6f558..93f2afec6 100644 --- a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/high-value-orders.md +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/high-value-orders.md @@ -1,7 +1,7 @@ --- title: High Value Orders description: Practice using EXCEPT to identify potential technical book opportunities -order: 110 +order: 130 type: challenge setup: | ```sql @@ -39,9 +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. - -We have the `customer` and `sale` tables. +Given the following three tables you are required to write a query to find the orders worth more than or equal to $500. > `customer` table has the list of customers. @@ -66,13 +64,13 @@ 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 the orders above $500 along with the information of the customers. Your output should have the following columns: +Your output should contain the orders with amount greater than or equal to `$500` and the following columns for each order: - `customer_name` - `order_amount` - `order_date` -The output should be ordered by the `order_amount` in descending order. +Results should be sorted by `order_amount` in descending order. ## Expected Output 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 e4dac2823..58481e365 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,7 +39,7 @@ setup: | ``` --- -Our bookstore has some marketing budget. The marketing team wants to send coupons to customers who have never placed an order. They have asked us to extract the list of customers for them. +Given the following data in `customer` and `sale` tables, we need to find the customers who have never placed an order. We have the `customer` and `sale` tables. diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/multi-section-authors.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/multi-section-authors.md index 56734b184..2ebd337a7 100644 --- a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/multi-section-authors.md +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/multi-section-authors.md @@ -1,7 +1,7 @@ --- title: Multi-Section Authors description: Practice creating a report with multiple tables -order: 120 +order: 180 type: challenge setup: | ```sql diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/recent-orders.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/recent-orders.md index f37d7ddc7..adca2166c 100644 --- a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/recent-orders.md +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/recent-orders.md @@ -39,9 +39,7 @@ setup: | ``` --- -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. +Given the following data in `customer` and `sale` tables, we want to track the recent orders in our bookstore. > `customer` table has the list of customers. @@ -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 the 3 most recent orders along with the information of the customers. Your output should have the following columns: +Write a query to find the 3 most recent orders along with customer information. Your output should have the following columns: - `customer_name` - `order_date` diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/referred-customers.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/referred-customers.md new file mode 100644 index 000000000..8b22a3809 --- /dev/null +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/referred-customers.md @@ -0,0 +1,66 @@ +--- +title: Referred Customers +description: Practice using joins to find customers who have been referred by another customer +order: 150 +type: challenge +setup: | + ```sql + CREATE TABLE customer ( + id INT PRIMARY KEY, + name VARCHAR(255), + email VARCHAR(255), + referred_by INT + ); + + INSERT INTO customer (id, name, email, referred_by) + VALUES + (1, 'Alice Smith', 'alice@example.com', NULL), + (2, 'Bob Johnson', 'bob@example.com', 1), + (3, 'Carol White', 'carol@example.com', 1), + (4, 'David Brown', 'david@example.com', 2), + (5, 'Emily Davis', 'emily@example.com', NULL); + ``` +--- + +The bookstore runs a referral program where existing customers can refer new customers. Each referred customer records the ID of the referring customer in the database. + +Write a query to find the names and email addresses of customers who have referred at least one other customer. + +You can use the data from the existing `customer` table. + +| customer_id | name | email | referred_by | +| ----------- | ----------- | ----------------- | ----------- | +| 1 | Alice Smith | alice@example.com | NULL | +| 2 | Bob Johnson | bob@example.com | 1 | +| 3 | Carol White | carol@example.com | 1 | +| 4 | David Brown | david@example.com | 2 | +| 5 | Emily Davis | emily@example.com | NULL | + +Your output should contain the following columns: + +- `name` +- `email` +- `referred_by` (name of the customer or '- None -') + +> **Tip:** You can use `COALESCE` function for `referred_by` column. + +## Expected Output + +| name | email | referred_by | +| ----------- | ----------------- | ----------- | +| Carol White | carol@example.com | Alice Smith | +| Bob Johnson | bob@example.com | Alice Smith | +| David Brown | david@example.com | Bob Johnson | +| Emily Davis | emily@example.com | - None - | +| Alice Smith | alice@example.com | - None - | + +## Solution + +```sql +SELECT + c.name, + c.email, + COALESCE(r.name, '- None -') AS referred_by +FROM customer c +LEFT JOIN customer r ON c.referred_by = r.id; +``` diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/specific-book-customers.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/specific-book-customers.md new file mode 100644 index 000000000..d7288f8e6 --- /dev/null +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/specific-book-customers.md @@ -0,0 +1,118 @@ +--- +title: Specific Book Customers +description: Practice using joins to find customers who have bought a specific book +order: 140 +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, + book_id INT, + order_date DATE, + total_amount DECIMAL(10,2) + ); + + CREATE TABLE book ( + id INT PRIMARY KEY, + title VARCHAR(255), + author VARCHAR(255) + ); + + INSERT INTO book (id, title, author) + VALUES + (1, 'The Great Gatsby', 'F. Scott Fitzgerald'), + (2, 'To Kill a Mockingbird', 'Harper Lee'), + (3, '1984', 'George Orwell'), + (4, 'Pride and Prejudice', 'Jane Austen'), + (5, 'The Catcher in the Rye', 'J.D. Salinger'); + + 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, book_id, order_date, total_amount) + VALUES + (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); + ``` +--- + +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". + +> `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 made to customers. + +| 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 | + +> `book` table has the list of books. + +| id | title | author | +| --- | ---------------------- | ------------------- | +| 1 | The Great Gatsby | F. Scott Fitzgerald | +| 2 | To Kill a Mockingbird | Harper Lee | +| 3 | 1984 | George Orwell | +| 4 | Pride and Prejudice | Jane Austen | +| 5 | The Catcher in the Rye | J.D. Salinger | + +## Expected Output + +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. + +| id | name | title | purchase_date | +| --- | ---------- | --------------------- | ----------------- | +| 1 | John Doe | To Kill a Mockingbird | November 15, 2024 | +| 2 | Jane Smith | To Kill a Mockingbird | November 11, 2024 | + +## Solution + +```sql +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' +``` diff --git a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/trending-tech-books.md b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/trending-tech-books.md index b2db28496..378063742 100644 --- a/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/trending-tech-books.md +++ b/src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/trending-tech-books.md @@ -1,7 +1,7 @@ --- title: Trending Tech Books description: Practice using EXCEPT to identify potential technical book opportunities -order: 100 +order: 200 type: challenge setup: | ```sql @@ -59,7 +59,7 @@ You have access to two tables: `trending_book` which tracks currently popular bo | 4 | Python Crash Course | Python | 34.99 | | 5 | Head First Java | Java | 44.99 | -Write a query to find the technical books that are not currently trending. This will help the publisher to review the books that are not currently popular and consider creating new editions or improving the existing ones. +Write a query to find the technical books that are not currently trending. **Hint:** you can use one of the set operations. ## Expected Results