Add new challenge

feat/course
Kamran Ahmed 3 weeks ago
parent d917257a2b
commit ec399cec41
  1. 2
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/expensive-books.md
  2. 2
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/multi-section-authors.md
  3. 49
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/set-operator-queries.md
  4. 82
      src/data/courses/sql-mastery/chapters/multi-table-queries/lessons/trending-tech-books.md

@ -1,7 +1,7 @@
---
title: Expensive Books
description: Practice creating a report with multiple tables
order: 100
order: 110
type: challenge
setup: |
```sql

@ -1,7 +1,7 @@
---
title: Multi-Section Authors
description: Practice creating a report with multiple tables
order: 100
order: 120
type: challenge
setup: |
```sql

@ -219,51 +219,6 @@ The output will be:
Notice how it took the `writer_name` column alias from the first query for the name of the column in the result set.
## Challenge Time! 🎯
Using the provided tables, write queries to:
1. Find all books that cost exactly $19.99 across all categories
2. List authors who have written both fiction and non-fiction books
3. Find summer reads that aren't in our fiction collection
<details>
<summary>Solution</summary>
```sql
-- 1. Books that cost $19.99
SELECT title, author, 'Fiction' as category
FROM fiction_book
WHERE price = 19.99
UNION
SELECT title, author, 'Non-Fiction' as category
FROM non_fiction_book
WHERE price = 19.99;
-- 2. Authors in both categories
SELECT author FROM fiction_book
INTERSECT
SELECT author FROM non_fiction_book;
-- 3. Summer reads not in fiction
SELECT title, author FROM summer_reads
EXCEPT
SELECT title, author FROM fiction_book;
```
</details>
## Key Takeaways
- Use UNION to combine results and remove duplicates
- Use UNION ALL when you want to keep duplicates (it's faster than UNION)
- Use INTERSECT to find common rows between results
- Use EXCEPT to find rows in one result that aren't in another
- Column count, order, and data types must match across queries
- These operations are particularly useful when working with similar but separate data sets
Remember: Choose the appropriate set operation based on your needs:
---
- UNION/UNION ALL for combining results
- INTERSECT for finding common elements
- EXCEPT for finding differences
Remember to practice the queries above in the editor. Feel free to create your own tables and data to practice with. In the next lesson we will look at Views.

@ -0,0 +1,82 @@
---
title: Trending Tech Books
description: Practice using EXCEPT to identify potential technical book opportunities
order: 100
type: challenge
setup: |
```sql
CREATE TABLE trending_book (
title VARCHAR(255),
author VARCHAR(255),
rating DECIMAL(3,2),
votes INT
);
CREATE TABLE tech_book (
id INT PRIMARY KEY,
title VARCHAR(255),
category VARCHAR(100),
price DECIMAL(10,2)
);
INSERT INTO trending_book (title, author, rating, votes)
VALUES
('Clean Code', 'Robert C. Martin', 4.8, 1205),
('The Pragmatic Programmer', 'Andy Hunt', 4.9, 890),
('Dune', 'Frank Herbert', 4.7, 2500),
('Project Hail Mary', 'Andy Weir', 4.8, 1800),
('Neuromancer', 'William Gibson', 4.5, 1600);
INSERT INTO tech_book (id, title, category, price)
VALUES
(1, 'Clean Code', 'Software Engineering', 44.99),
(2, 'The Pragmatic Programmer', 'Software Engineering', 39.99),
(3, 'JavaScript: The Good Parts', 'JavaScript', 29.99),
(4, 'Python Crash Course', 'Python', 34.99),
(5, 'Head First Java', 'Java', 44.99);
```
---
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.
> `trending_book` table shows currently trending books:
| title | author | rating | votes |
| ------------------------ | ---------------- | ------ | ----- |
| Clean Code | Robert C. Martin | 4.8 | 1205 |
| The Pragmatic Programmer | Andy Hunt | 4.9 | 890 |
| Dune | Frank Herbert | 4.7 | 2500 |
| Project Hail Mary | Andy Weir | 4.8 | 1800 |
| Neuromancer | William Gibson | 4.5 | 1600 |
> `tech_book` table contains the technology book catalog:
| id | title | category | price |
| --- | -------------------------- | -------------------- | ----- |
| 1 | Clean Code | Software Engineering | 44.99 |
| 2 | The Pragmatic Programmer | Software Engineering | 39.99 |
| 3 | JavaScript: The Good Parts | JavaScript | 29.99 |
| 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.
## Expected Results
Your query should return:
| title |
| -------------------------- |
| Python Crash Course |
| Head First Java |
| JavaScript: The Good Parts |
## Solution
```sql
SELECT title
FROM tech_book
EXCEPT
SELECT title
FROM trending_book;
```
Loading…
Cancel
Save