From 22d60f51e1b1bf7eff61ff8c291ac0748a5c9ef6 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 2 Jan 2025 13:49:38 +0000 Subject: [PATCH] Update inactive customer query --- .../lessons/expensive-books.md | 63 +++++++++++++++---- .../lessons/trending-tech-books.md | 44 ++++++++++--- 2 files changed, 89 insertions(+), 18 deletions(-) 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 ef94e502a..b4b967cdf 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 @@ -50,9 +50,9 @@ setup: | ``` --- -Given the following three tables with the given data: +The bookstore manager wants to analyze premium-priced books across all sections of the store. They're particularly interested in books priced above $20 to understand the distribution of higher-priced inventory across different categories. This information will help them make informed decisions about pricing strategies and premium book displays. -> `fiction_book` table has the following data: +Given the following data in table `fiction_book` | id | title | author | price | | --- | ----------------------- | ------------------- | ----- | @@ -62,7 +62,7 @@ Given the following three tables with the given data: | 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 | | --- | ---------------------------------- | ----------------- | ----- | @@ -72,7 +72,7 @@ Given the following three tables with the given data: | 4 | Pride and Prejudice: A Study Guide | John Smith | 12.99 | | 5 | The Doors of Perception | Aldous Huxley | 12.99 | -> `summer_read` table has the following data: +And the following data in table `summer_read` | id | title | author | price | | --- | ---------------- | ------------------- | ----- | @@ -80,13 +80,16 @@ Given the following three tables with the given data: | 2 | Sapiens | Yuval Noah Harari | 24.99 | | 3 | The Art of War | Sun Tzu | 19.99 | -Write a query to find the books in all three tables that cost more than $20 along with their section i.e. `Fiction`, `Non-Fiction`, and `Summer Read`. +Write a query to identify all books priced above $20 across all sections. The manager wants to see: -> It's okay if the books are repeated in the result set. Important thing is to show the section and the book details. +- The book's ID +- The title +- The price +- Which section it belongs to (Fiction, Non-Fiction, or Summer Read) -## Expected Results +Note: Some books may appear multiple times if they're featured in different sections. -Your output should look like this: +## Expected Output | id | title | price | section | | --- | ----------------------- | ----- | ----------- | @@ -101,11 +104,49 @@ Your output should look like this: ```sql SELECT id, title, price, 'Fiction' as section -FROM fiction_book WHERE price > 20 +FROM fiction_book +WHERE price > 20 UNION SELECT id, title, price, 'Non-Fiction' as section -FROM non_fiction_book WHERE price > 20 +FROM non_fiction_book +WHERE price > 20 UNION SELECT id, title, price, 'Summer Read' as section -FROM summer_read WHERE price > 20 +FROM summer_read +WHERE price > 20 +ORDER BY section, price DESC; ``` + +### Explanation + +Let's break down how this query works: + +First, we get expensive books from the fiction section: + +```sql +SELECT id, title, price, 'Fiction' as section +FROM fiction_book +WHERE price > 20 +``` + +Then we use `UNION` to combine results with expensive non-fiction books: + +```sql +UNION +SELECT id, title, price, 'Non-Fiction' as section +FROM non_fiction_book +WHERE price > 20 +``` + +And finally, we add expensive books from the summer reading list: + +```sql +UNION +SELECT id, title, price, 'Summer Read' as section +FROM summer_read +WHERE price > 20 +``` + +The `UNION` operator combines the results from all three queries while removing any duplicates. We add a literal string value as 'section' to identify which table each book comes from. + +This query helps the manager understand the distribution of premium-priced books across different sections, which can inform decisions about pricing strategies and store displays. 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 378063742..bddd473b7 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 @@ -37,9 +37,9 @@ setup: | ``` --- -You have access to two tables: `trending_book` which tracks currently popular books across all genres, and `tech_book` which lists programming and technology books. +The bookstore's marketing team wants to boost sales in the technology section. They've noticed some tech books aren't getting as much attention as others and want to identify which technical books aren't currently trending. This information will help them plan targeted promotions and display adjustments to increase visibility for these underperforming titles. -> `trending_book` table shows currently trending books: +Given the following data in table `trending_book` | title | author | rating | votes | | ------------------------ | ---------------- | ------ | ----- | @@ -49,7 +49,7 @@ You have access to two tables: `trending_book` which tracks currently popular bo | Project Hail Mary | Andy Weir | 4.8 | 1800 | | Neuromancer | William Gibson | 4.5 | 1600 | -> `tech_book` table contains the technology book catalog: +And the following data in table `tech_book` | id | title | category | price | | --- | -------------------------- | -------------------- | ----- | @@ -59,11 +59,11 @@ 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. **Hint:** you can use one of the set operations. +Write a query to identify technical books that aren't currently in the trending list. The marketing team wants to see: -## Expected Results +- The title of each non-trending technical book -Your query should return: +## Expected Output | title | | -------------------------- | @@ -78,5 +78,35 @@ SELECT title FROM tech_book EXCEPT SELECT title -FROM trending_book; +FROM trending_book +ORDER BY title; ``` + +### Explanation + +Let's break down how this query works: + +First, we get all titles from the technical books catalog: + +```sql +SELECT title +FROM tech_book +``` + +Then we use `EXCEPT` to remove any titles that appear in the trending books list: + +```sql +EXCEPT +SELECT title +FROM trending_book +``` + +The `EXCEPT` operator returns all rows from the first query that don't appear in the results of the second query. This effectively gives us all technical books that aren't trending. + +Finally, we order the results alphabetically: + +```sql +ORDER BY title +``` + +This query helps the marketing team identify which technical books might need additional promotion or better placement in the store. They can use this information to create targeted marketing campaigns or special displays for these books to increase their visibility and sales.