From 49d0bb68abdd01021aa350618e2db883b31efdbd Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Wed, 25 Dec 2024 12:44:16 +0000 Subject: [PATCH] Add challenge --- .../sql-basics/lessons/challenge-5.md | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/data/courses/sql/chapters/sql-basics/lessons/challenge-5.md diff --git a/src/data/courses/sql/chapters/sql-basics/lessons/challenge-5.md b/src/data/courses/sql/chapters/sql-basics/lessons/challenge-5.md new file mode 100644 index 000000000..7e29ca692 --- /dev/null +++ b/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 |