Add new challenges

pull/8127/head
Kamran Ahmed 2 months ago
parent 064a9422d4
commit 44c87f299b
  1. 86
      src/data/courses/sql-mastery/chapters/scalar-functions/lessons/book-performance.md
  2. 46
      src/data/courses/sql-mastery/chapters/scalar-functions/lessons/book-sale-categories.md

@ -0,0 +1,86 @@
---
title: Book Performance
description: Practice using CASE expressions and GROUP BY for book analysis
order: 220
type: challenge
setup: |
```sql
CREATE TABLE book (
id INT PRIMARY KEY,
title VARCHAR(100),
category VARCHAR(50),
rating DECIMAL(3,1),
published_year INT,
total_reviews INT
);
INSERT INTO book (id, title, category, rating, published_year, total_reviews) VALUES
(1, 'SQL Basics', 'Programming', 4.5, 2020, 120),
(2, 'Advanced Queries', 'Programming', 4.8, 2021, 85),
(3, 'Database Design', 'Database', 4.2, 2019, 150),
(4, 'Query Optimization', 'Database', 4.6, 2022, 65),
(5, 'Data Modeling', 'Database', 2.9, 2021, 45),
(6, 'PostgreSQL Tips', 'Programming', 4.7, 2023, 95),
(7, 'NoSQL Concepts', 'Database', 4.3, 2022, 75),
(8, 'SQL for Beginners', 'Programming', 4.4, 2020, 200);
```
---
The book store wants to analyze their book sales. They need a report that shows book performance by category and rating.
Given the following data in table `book`:
| id | title | category | rating | total_reviews |
| --- | ------------------ | ----------- | ------ | ------------- |
| 1 | SQL Basics | Programming | 4.5 | 120 |
| 2 | Advanced Queries | Programming | 4.8 | 85 |
| 3 | Database Design | Database | 4.2 | 150 |
| 4 | Query Optimization | Database | 4.6 | 65 |
| 5 | Data Modeling | Database | 2.9 | 45 |
| 6 | PostgreSQL Tips | Programming | 4.7 | 95 |
| 7 | NoSQL Concepts | Database | 4.3 | 75 |
| 8 | SQL for Beginners | Programming | 4.4 | 200 |
Write a query that shows:
- Category
- Rating group:
- `Outstanding` if rating >= 4.5
- `Good` if rating >= 4.0
- `Average` if rating >= 3.5
- `Poor` if rating >= 3.0
- Number of books in each group
## Expected Output
| category | rating_group | book_count |
| ----------- | ------------ | ---------- |
| Database | Poor | 1 |
| Database | Outstanding | 1 |
| Database | Good | 2 |
| Programming | Outstanding | 3 |
| Programming | Good | 1 |
## Solution
```sql
SELECT
category,
CASE
WHEN rating >= 4.5 THEN 'Outstanding'
WHEN rating >= 4.0 THEN 'Good'
WHEN rating >= 3.5 THEN 'Average'
ELSE 'Poor'
END as rating_group,
COUNT(*) as book_count
FROM book
GROUP BY
category,
CASE
WHEN rating >= 4.5 THEN 'Outstanding'
WHEN rating >= 4.0 THEN 'Good'
WHEN rating >= 3.5 THEN 'Average'
ELSE 'Poor'
END
ORDER BY category, rating_group DESC;
```

@ -24,18 +24,18 @@ setup: |
```
---
A library needs to analyze their SQL book collection. They want a report that categorizes books based on various criteria.
The bookstore manager wants to analyze the SQL book collection. They want a report that categorizes books based on various criteria.
Given the following data in table `book`:
| id | title | page_count | publish_year | copies_in_stock | monthly_rentals |
| --- | ------------------ | ---------- | ------------ | --------------- | --------------- |
| 1 | SQL Basics | 250 | 2020 | 5 | 12 |
| 2 | Advanced Queries | 450 | 2021 | 3 | 8 |
| 3 | Database Design | 350 | 2019 | 10 | 15 |
| 4 | Query Optimization | 275 | 2022 | 2 | 6 |
| 5 | Data Modeling | 400 | 2021 | 1 | 4 |
| 6 | PostgreSQL Tips | 200 | 2023 | 8 | 20 |
| title | page_count | copies_in_stock | monthly_rentals |
| ------------------ | ---------- | --------------- | --------------- |
| SQL Basics | 250 | 5 | 12 |
| Advanced Queries | 450 | 3 | 8 |
| Database Design | 350 | 10 | 15 |
| Query Optimization | 275 | 2 | 6 |
| Data Modeling | 400 | 1 | 4 |
| PostgreSQL Tips | 200 | 8 | 20 |
Write a query that shows:
@ -44,10 +44,6 @@ Write a query that shows:
- `Short` if page_count < 300
- `Medium` if page_count between 300 and 400
- `Long` if page_count > 400
- Publication status:
- `Recent` if publish_year >= 2022
- `Current` if publish_year is 2020 or 2021
- `Older` if publish_year < 2020
- Stock status:
- `Low` if copies_in_stock < 3
- `Medium` if copies_in_stock between 3 and 7
@ -59,15 +55,14 @@ Write a query that shows:
## Expected Output
| title | length_category | publication_status | stock_status | popularity |
| ------------------ | --------------- | ------------------ | ------------ | ------------- |
| PostgreSQL Tips | Short | Recent | High | High Demand |
| Database Design | Medium | Older | High | High Demand |
| SQL Basics | Short | Current | Medium | Medium Demand |
| Advanced Queries | Long | Current | Medium | Medium Demand |
| Query Optimization | Short | Recent | Low | Medium Demand |
Data Modeling Medium Current Low Low Demand
| title | length_category | stock_status | popularity |
| ------------------ | --------------- | ------------ | ------------- |
| PostgreSQL Tips | Short | High | High Demand |
| Database Design | Medium | High | High Demand |
| SQL Basics | Short | Medium | Medium Demand |
| Advanced Queries | Long | Medium | Medium Demand |
| Query Optimization | Short | Low | Medium Demand |
| Data Modeling | Medium | Low | Low Demand |
## Solution
@ -79,11 +74,6 @@ SELECT
WHEN page_count <= 400 THEN 'Medium'
ELSE 'Long'
END as length_category,
CASE
WHEN publish_year >= 2022 THEN 'Recent'
WHEN publish_year >= 2020 THEN 'Current'
ELSE 'Older'
END as publication_status,
CASE
WHEN copies_in_stock < 3 THEN 'Low'
WHEN copies_in_stock <= 7 THEN 'Medium'
@ -94,6 +84,6 @@ SELECT
WHEN monthly_rentals >= 6 THEN 'Medium Demand'
ELSE 'Low Demand'
END as popularity
FROM books
FROM book
ORDER BY monthly_rentals DESC;
```

Loading…
Cancel
Save