Add challenges

pull/8127/head
Kamran Ahmed 5 months ago
parent e90acb2b3c
commit a509202e9c
  1. 64
      src/data/courses/sql/chapters/sql-basics/lessons/challenge-1.md
  2. 55
      src/data/courses/sql/chapters/sql-basics/lessons/challenge-2.md
  3. 54
      src/data/courses/sql/chapters/sql-basics/lessons/challenge-3.md
  4. 28
      src/data/courses/sql/chapters/sql-basics/lessons/comments.md

@ -3,42 +3,52 @@ title: Challenge 1
description: Write a SQL query to find the total number of orders in the `orders` table.
order: 300
type: challenge
defaultValue: |
SELECT
*
FROM
orders;
SELECT
COUNT(*)
FROM
orders;
initSteps:
- CREATE TABLE orders (
- CREATE TABLE customers (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
order_date DATE,
total DECIMAL(10, 2)
name TEXT,
phone TEXT,
email TEXT
);
- INSERT INTO orders (id, customer_id, order_date, total)
- INSERT INTO customers (id, name, phone, email)
VALUES
(1, 1, '2021-01-01', 100.00),
(2, 2, '2021-01-02', 200.00),
(3, 1, '2021-01-03', 300.00),
(4, 3, '2021-01-04', 400.00),
(5, 2, '2021-01-05', 500.00);
(1, 'John', '555-123-4567', 'john@example.com'),
(2, 'Jane', '555-987-6543', 'jane@example.com'),
(3, 'Bob', NULL, 'bob@example.com'),
(4, 'David', NULL, 'david@example.com'),
(5, 'Charlie', '555-555-5555', 'charlie@example.com');
expectedResults:
- columns: [total_orders]
- columns: [Full Name, Email]
values:
- [5]
- ['John', 'john@example.com']
- ['Jane', 'jane@example.com']
- ['Bob', 'bob@example.com']
- ['David', 'david@example.com']
- ['Charlie', 'charlie@example.com']
---
Write a SQL query to find the total number of orders in the `orders` table.
Given the following customers `customers` table:
| id | name | phone | email |
| --- | ------- | ------------ | ------------------- |
| 1 | John | 555-123-4567 | john@example.com |
| 2 | Jane | 555-987-6543 | jane@example.com |
| 3 | Bob | NULL | bob@example.com |
| 4 | David | NULL | david@example.com |
| 5 | Charlie | 555-555-5555 | charlie@example.com |
Write a query to get the `name` and `email` of all customers.
## Result
Your query should return a single column with the total number of orders in the `orders` table.
Your output should look like this:
| Full Name | Email |
| --------- | ------------------- |
| John | john@example.com |
| Jane | jane@example.com |
| Bob | bob@example.com |
| David | david@example.com |
| Charlie | charlie@example.com |
- The column name should be `total_orders`.
- The value should be the total number of orders in the `orders` table.
- The query should return a single row with the total number of orders in the `orders` table.
Notice that the `name` column is renamed to `Full Name` and the `email` column is renamed to `Email`.

@ -0,0 +1,55 @@
---
title: Challenge 2
description: Write a SQL query to find the total number of orders in the `orders` table.
order: 300
type: challenge
initSteps:
- CREATE TABLE books (
id INTEGER PRIMARY KEY,
title TEXT,
price REAL,
discount REAL,
markup REAL
);
- INSERT INTO books (id, title, price, discount, markup)
VALUES
(1, 'The Great Gatsby', 10.99, 0.10, 1.10),
(2, 'To Kill a Mockingbird', 12.99, 0.15, 1.15),
(3, '1984', 8.99, 0.05, 1.05),
(4, 'The Catcher in the Rye', 11.99, 0.12, 1.12),
(5, 'The Hobbit', 9.99, 0.08, 1.15);
expectedResults:
- columns: [title, final_price]
values:
- ['The Great Gatsby', 11.99]
- ['To Kill a Mockingbird', 13.99]
- ['1984', 9.99]
- ['The Catcher in the Rye', 12.99]
- ['The Hobbit', 11.06]
---
Given the following `books` table:
| id | title | price | discount | markup |
| --- | ---------------------- | ----- | -------- | ------ |
| 1 | The Great Gatsby | 10.99 | 0.10 | 1.10 |
| 2 | To Kill a Mockingbird | 12.99 | 0.15 | 1.15 |
| 3 | 1984 | 8.99 | 0.05 | 1.05 |
| 4 | The Catcher in the Rye | 11.99 | 0.12 | 1.12 |
| 5 | The Hobbit | 9.99 | 0.08 | 1.15 |
Write an SQL query that returns the `title`, `final_price` of each book. The `final_price` is calculated using the following formula:
```
final_price = price + markup - discount
```
## Expected Results
| title | final_price |
| ---------------------- | ----------- |
| The Great Gatsby | 11.99 |
| To Kill a Mockingbird | 13.99 |
| 1984 | 9.99 |
| The Catcher in the Rye | 12.99 |
| The Hobbit | 11.06 |

@ -0,0 +1,54 @@
---
title: Challenge 3
description: Write a SQL query to find the total number of orders in the `orders` table.
order: 300
type: challenge
initSteps:
- CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
total_price REAL,
status TEXT
);
- INSERT INTO orders (id, customer_id, total_price, status)
VALUES
(1, 1, 100.00, 'shipped'),
(2, 3, 150.00, 'pending'),
(3, 1, 200.00, 'shipped'),
(4, 3, 250.00, 'cancelled'),
(5, 2, 300.00, 'shipped'),
(6, 1, 300.00, 'pending'),
(7, 2, 300.00, 'shipped'),
(8, 3, 300.00, 'pending'),
(9, 1, 300.00, 'pending'),
(10, 2, 300.00, 'shipped');
expectedResults:
- columns: [customer_id]
values:
- [3]
- [1]
---
Given the following `orders` table:
| id | customer_id | total_price | status |
| --- | ----------- | ----------- | --------- |
| 1 | 1 | 100.00 | shipped |
| 2 | 3 | 150.00 | pending |
| 3 | 1 | 200.00 | shipped |
| 4 | 3 | 250.00 | cancelled |
| 5 | 2 | 300.00 | shipped |
| 6 | 1 | 300.00 | pending |
| 7 | 2 | 300.00 | shipped |
| 8 | 3 | 300.00 | pending |
| 9 | 1 | 300.00 | pending |
| 10 | 2 | 300.00 | shipped |
Write an SQL query to get a unique list of `customer_id` values where the status is `pending`.
## Expected Results
| customer_id |
| ----------- |
| 1 |
| 3 |

@ -17,13 +17,13 @@ Single line comments are created using `--` followed by the comment text. For ex
-- They are ignored by the database.
SELECT * FROM customers;
-- You can use them to comment
-- You can use them to comment
-- out parts of your query.
SELECT * FROM customers; -- WHERE id = 1;
-- Add notes to your query for later reference.
SELECT *
FROM customers
SELECT *
FROM customers
WHERE country = 'USA'; -- USA users only
```
@ -39,18 +39,16 @@ Multi line comments are created using `/*` followed by the comment text and `*/`
SELECT * FROM customers;
/*
-- Other queries for our database
SELECT * FROM customers;
SELECT * FROM orders;
SELECT * FROM books;
SELECT * FROM customers;
SELECT * FROM orders;
SELECT * FROM books;
*/
```
You can also use them to comment out parts of your query. e.g. here we are commenting out the country condition so we are only getting orders that are shipped.
-- You can also use them to comment
-- out parts of your query. e.g.
-- here we are commenting out the
-- country condition so we are only
-- getting orders that are shipped.
SELECT *
FROM orders
```sql
SELECT *
FROM orders
WHERE /* country = 'USA' AND */ status = 'shipped';
```
```

Loading…
Cancel
Save