parent
eb954549af
commit
05888fc711
3 changed files with 178 additions and 89 deletions
@ -1,89 +0,0 @@ |
||||
--- |
||||
title: Above Average Books |
||||
description: Practice using scalar subqueries to find books above average price in their category |
||||
order: 140 |
||||
type: challenge |
||||
setup: | |
||||
```sql |
||||
CREATE TABLE book ( |
||||
id INT PRIMARY KEY, |
||||
title VARCHAR(100), |
||||
category VARCHAR(50), |
||||
price DECIMAL(10,2), |
||||
published_date DATE |
||||
); |
||||
|
||||
INSERT INTO book (id, title, category, price, published_date) VALUES |
||||
(1, 'SQL Basics', 'Programming', 29.99, '2024-01-15'), |
||||
(2, 'Advanced SQL', 'Programming', 49.99, '2024-02-20'), |
||||
(3, 'Data Science', 'Data Analysis', 39.99, '2024-01-10'), |
||||
(4, 'Web Development', 'Programming', 34.99, '2024-03-01'), |
||||
(5, 'Statistics 101', 'Data Analysis', 24.99, '2024-02-15'), |
||||
(6, 'Machine Learning', 'Data Analysis', 44.99, '2024-03-15'), |
||||
(7, 'Database Design', 'Programming', 39.99, '2024-01-20'), |
||||
(8, 'Python Basics', 'Programming', 29.99, '2024-02-01'); |
||||
``` |
||||
--- |
||||
|
||||
The bookstore manager wants to identify books that are priced above the average price in their respective categories, but only for books published in 2024. This information will help in adjusting pricing strategies. |
||||
|
||||
Given the following data in table `book`: |
||||
|
||||
| id | title | category | price | published_date | |
||||
| --- | ---------------- | ------------- | ----- | -------------- | |
||||
| 1 | SQL Basics | Programming | 29.99 | 2024-01-15 | |
||||
| 2 | Advanced SQL | Programming | 49.99 | 2024-02-20 | |
||||
| 3 | Data Science | Data Analysis | 39.99 | 2024-01-10 | |
||||
| 4 | Web Development | Programming | 34.99 | 2024-03-01 | |
||||
| 5 | Statistics 101 | Data Analysis | 24.99 | 2024-02-15 | |
||||
| 6 | Machine Learning | Data Analysis | 44.99 | 2024-03-15 | |
||||
| 7 | Database Design | Programming | 39.99 | 2024-01-20 | |
||||
| 8 | Python Basics | Programming | 29.99 | 2024-02-01 | |
||||
|
||||
Write a query that shows: |
||||
|
||||
- Book title |
||||
- Category |
||||
- Book price |
||||
- Category average price (rounded to 2 decimal places) |
||||
- Price difference from category average (rounded to 2 decimal places) |
||||
|
||||
Only include books that are: |
||||
|
||||
1. Published in 2024 |
||||
2. Priced above their category's average price |
||||
|
||||
## Expected Output |
||||
|
||||
| title | category | price | category_avg | price_diff | |
||||
| ---------------- | ------------- | ----- | ------------ | ---------- | |
||||
| Machine Learning | Data Analysis | 44.99 | 36.66 | 8.33 | |
||||
| Data Science | Data Analysis | 39.99 | 36.66 | 3.33 | |
||||
| Advanced SQL | Programming | 49.99 | 36.99 | 13.00 | |
||||
| Database Design | Programming | 39.99 | 36.99 | 3.00 | |
||||
|
||||
## Solution |
||||
|
||||
```sql |
||||
SELECT |
||||
b1.title, |
||||
b1.category, |
||||
b1.price, |
||||
ROUND(avg_price, 2) AS category_avg, |
||||
ROUND(b1.price - avg_price, 2) AS price_diff |
||||
FROM book b1 |
||||
INNER JOIN ( |
||||
-- Subquery to calculate average price per category for 2024 |
||||
SELECT |
||||
category, |
||||
AVG(price) AS avg_price |
||||
FROM book |
||||
WHERE EXTRACT(YEAR FROM published_date) = 2024 |
||||
GROUP BY category |
||||
) AS ca ON b1.category = ca.category |
||||
WHERE |
||||
EXTRACT(YEAR FROM b1.published_date) = 2024 -- Filter for books published in 2024 |
||||
AND b1.price > ca.avg_price -- Filter books above category average |
||||
ORDER BY |
||||
b1.category, price_diff DESC; -- Sort results |
||||
``` |
@ -0,0 +1,86 @@ |
||||
--- |
||||
title: Books Above Average |
||||
description: Practice using simple subqueries to find books above average price |
||||
order: 141 |
||||
type: challenge |
||||
setup: | |
||||
```sql |
||||
CREATE TABLE book ( |
||||
id INT PRIMARY KEY, |
||||
title VARCHAR(100), |
||||
category VARCHAR(50), |
||||
price DECIMAL(10,2), |
||||
stock INT |
||||
); |
||||
|
||||
INSERT INTO book (id, title, category, price, stock) VALUES |
||||
(1, 'SQL Basics', 'Programming', 29.99, 10), |
||||
(2, 'Advanced SQL', 'Programming', 49.99, 5), |
||||
(3, 'Data Science', 'Data Analysis', 39.99, 8), |
||||
(4, 'Web Development', 'Programming', 34.99, 12), |
||||
(5, 'Statistics 101', 'Data Analysis', 24.99, 15), |
||||
(6, 'Machine Learning', 'Data Analysis', 44.99, 6); |
||||
``` |
||||
--- |
||||
|
||||
The bookstore manager wants to identify expensive books that might need a price adjustment. They need to find all books that are priced above the average book price. |
||||
|
||||
Given the following data in table `book`: |
||||
|
||||
| id | title | category | price | stock | |
||||
| -- | --------------- | ------------- | ----- | ----- | |
||||
| 1 | SQL Basics | Programming | 29.99 | 10 | |
||||
| 2 | Advanced SQL | Programming | 49.99 | 5 | |
||||
| 3 | Data Science | Data Analysis | 39.99 | 8 | |
||||
| 4 | Web Development | Programming | 34.99 | 12 | |
||||
| 5 | Statistics 101 | Data Analysis | 24.99 | 15 | |
||||
| 6 | Machine Learning| Data Analysis | 44.99 | 6 | |
||||
|
||||
Write a query that shows: |
||||
|
||||
- Book title |
||||
- Category |
||||
- Price |
||||
- How much the price is above the average (rounded to 2 decimal places) |
||||
|
||||
Only include books that are priced above the average book price. |
||||
|
||||
## Expected Output |
||||
|
||||
| title | category | price | above_average | |
||||
| --------------- | ------------- | ----- | ------------- | |
||||
| Advanced SQL | Programming | 49.99 | 12.66 | |
||||
| Machine Learning| Data Analysis | 44.99 | 7.66 | |
||||
| Data Science | Data Analysis | 39.99 | 2.66 | |
||||
|
||||
> **Note:** The average book price is 37.33 |
||||
|
||||
## Solution |
||||
|
||||
```sql |
||||
SELECT |
||||
title, |
||||
category, |
||||
price, |
||||
ROUND(price - ( |
||||
SELECT AVG(price) |
||||
FROM book |
||||
), 2) as above_average |
||||
FROM book |
||||
WHERE price > ( |
||||
SELECT AVG(price) |
||||
FROM book |
||||
) |
||||
ORDER BY price DESC; |
||||
``` |
||||
|
||||
This challenge requires you to: |
||||
1. Calculate the average price using a subquery |
||||
2. Compare each book's price to this average |
||||
3. Show how much each book is above the average price |
||||
|
||||
The solution demonstrates the use of subqueries: |
||||
- In the SELECT clause to calculate the difference from average |
||||
- In the WHERE clause to filter for above-average prices |
||||
|
||||
--- |
@ -0,0 +1,92 @@ |
||||
--- |
||||
title: Low Stock by Category |
||||
description: Practice using correlated subqueries to find books with low stock in each category |
||||
order: 142 |
||||
type: challenge |
||||
setup: | |
||||
```sql |
||||
CREATE TABLE book ( |
||||
id INT PRIMARY KEY, |
||||
title VARCHAR(100), |
||||
category VARCHAR(50), |
||||
price DECIMAL(10,2), |
||||
stock INT |
||||
); |
||||
|
||||
INSERT INTO book (id, title, category, price, stock) VALUES |
||||
(1, 'SQL Basics', 'Programming', 29.99, 10), |
||||
(2, 'Advanced SQL', 'Programming', 49.99, 5), |
||||
(3, 'Data Science', 'Data Analysis', 39.99, 8), |
||||
(4, 'Web Development', 'Programming', 34.99, 3), |
||||
(5, 'Statistics 101', 'Data Analysis', 24.99, 15), |
||||
(6, 'Machine Learning', 'Data Analysis', 44.99, 6), |
||||
(7, 'Python Basics', 'Programming', 29.99, 12), |
||||
(8, 'R Programming', 'Data Analysis', 34.99, 4); |
||||
``` |
||||
--- |
||||
|
||||
The bookstore manager wants to identify books that have lower stock than the average stock level in their respective categories. This will help in planning reorders. |
||||
|
||||
Given the following data in table `book`: |
||||
|
||||
| id | title | category | price | stock | |
||||
| -- | --------------- | ------------- | ----- | ----- | |
||||
| 1 | SQL Basics | Programming | 29.99 | 10 | |
||||
| 2 | Advanced SQL | Programming | 49.99 | 5 | |
||||
| 3 | Data Science | Data Analysis | 39.99 | 8 | |
||||
| 4 | Web Development | Programming | 34.99 | 3 | |
||||
| 5 | Statistics 101 | Data Analysis | 24.99 | 15 | |
||||
| 6 | Machine Learning| Data Analysis | 44.99 | 6 | |
||||
| 7 | Python Basics | Programming | 29.99 | 12 | |
||||
| 8 | R Programming | Data Analysis | 34.99 | 4 | |
||||
|
||||
Write a query that shows books with stock levels below their category average. Show: |
||||
|
||||
- Book title |
||||
- Category |
||||
- Current stock |
||||
- Category average stock (rounded to 1 decimal place) |
||||
|
||||
## Expected Output |
||||
|
||||
| title | category | stock | category_avg | |
||||
| --------------- | ------------- | ----- | ------------ | |
||||
| Advanced SQL | Programming | 5 | 7.5 | |
||||
| Web Development | Programming | 3 | 7.5 | |
||||
| R Programming | Data Analysis | 4 | 8.3 | |
||||
| Machine Learning| Data Analysis | 6 | 8.3 | |
||||
|
||||
> **Note:** The query must use a correlated subquery to calculate the category average. |
||||
|
||||
## Solution |
||||
|
||||
```sql |
||||
SELECT |
||||
b1.title, |
||||
b1.category, |
||||
b1.stock, |
||||
ROUND(( |
||||
SELECT AVG(stock) |
||||
FROM book b2 |
||||
WHERE b2.category = b1.category |
||||
), 1) as category_avg |
||||
FROM book b1 |
||||
WHERE b1.stock < ( |
||||
SELECT AVG(stock) |
||||
FROM book b2 |
||||
WHERE b2.category = b1.category |
||||
) |
||||
ORDER BY b1.category, b1.stock; |
||||
``` |
||||
|
||||
This challenge requires you to: |
||||
1. Use a correlated subquery to calculate the average stock for each category |
||||
2. Compare each book's stock to its category average |
||||
3. Show only books with below-average stock |
||||
|
||||
The solution demonstrates: |
||||
- Using correlated subqueries in both SELECT and WHERE clauses |
||||
- Comparing individual values against group averages |
||||
- Working with a single table |
||||
|
||||
--- |
Loading…
Reference in new issue