Add challenge

feat/course
Kamran Ahmed 4 weeks ago
parent a57a4d731e
commit 49d0bb68ab
  1. 56
      src/data/courses/sql/chapters/sql-basics/lessons/challenge-5.md

@ -0,0 +1,56 @@
---
title: Challenge 5
description: Write a SQL query to find the total number of orders in the `orders` table.
order: 500
type: challenge
initSteps:
- CREATE TABLE books (
id INTEGER PRIMARY KEY,
title TEXT,
genre TEXT,
price REAL,
stock INTEGER,
year INTEGER
);
- INSERT INTO books (id, title, genre, price, stock, year)
VALUES
(1, 'The Great Adventure', 'Fiction', 15.99, 12, 2020),
(2, 'Mystery of the Night', 'Mystery', 12.50, 5, 2018),
(3, 'Science Explained', 'Science', 20.00, 0, 2019),
(4, 'Cooking Made Easy', 'Cooking', 18.99, 8, 2021),
(5, 'History Revisited', 'History', 10.99, 20, 2017),
(6, 'Fictional Reality', 'Fiction', 25.99, 3, 2022);
expectedResults:
- columns: [id, title, genre, price, stock, year]
values:
- [1, 'The Great Adventure', 'Fiction', 15.99, 12, 2020]
- [3, 'Science Explained', 'Science', 20.00, 0, 2019]
---
Given the following `books` table:
| id | title | genre | price | stock | year |
| --- | -------------------- | ------- | ----- | ----- | ---- |
| 1 | The Great Adventure | Fiction | 15.99 | 12 | 2020 |
| 2 | Mystery of the Night | Mystery | 12.50 | 5 | 2018 |
| 3 | Science Explained | Science | 20.00 | 0 | 2019 |
| 4 | Cooking Made Easy | Cooking | 18.99 | 8 | 2021 |
| 5 | History Revisited | History | 10.99 | 20 | 2017 |
| 6 | Fictional Reality | Fiction | 25.99 | 3 | 2022 |
Write a query to retrieve `books` that meet the following criteria and order them as specified:
- The `genre` must be either Fiction or Science.
- The `price` should be `>= 10` and `<= 20`.
Here are the sorting requirements:
- First, sort the results by **stock** in **descending order** (books with more stock appear first).
- If two books have the same stock, sort them by **price** in ascending order.
## Expected Results
| id | title | genre | price | stock | year |
| --- | ------------------- | ------- | ----- | ----- | ---- |
| 1 | The Great Adventure | Fiction | 15.99 | 12 | 2020 |
| 3 | Science Explained | Science | 20.00 | 0 | 2019 |
Loading…
Cancel
Save