Add inserting and updating challenges

pull/8127/head
Kamran Ahmed 2 months ago
parent a8bb3d5096
commit d39a66a7d8
  1. 54
      src/data/courses/sql/chapters/manipulating-data/lessons/inserting-customers.md
  2. 53
      src/data/courses/sql/chapters/manipulating-data/lessons/updating-bookstore.md

@ -0,0 +1,54 @@
---
title: Inserting Customers
description: Learn how to insert data into a table.
order: 100
type: challenge
setup: |
```sql
- CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT,
phone TEXT,
email TEXT
);
- INSERT INTO customers (id, name, phone, email)
VALUES
(1, 'John', '555-123-4567', 'john@example.com'),
(2, 'Jane', '555-987-6543', 'jane@example.com'),
(3, 'Bob', NULL, 'bob@example.com');
expectedResults:
- columns: [id, name, phone, email]
values:
- [1, 'John', '555-123-4567', 'john@example.com']
- [2, 'Jane', '555-987-6543', 'jane@example.com']
- [3, 'Bob', NULL, 'bob@example.com']
---
Given the following `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 |
Write a query to insert the following two new customers:
| Column | Customer 1 | Customer 2 |
| ------- | ----------------- | ----------------- |
| `id` | 4 | 5 |
| `name` | Alice | Smith |
| `phone` | 555-444-3333 | `NULL` |
| `email` | alice@example.com | smith@example.com |
## Result
After running your query, the `customers` table should look like this:
| 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 | Alice | 555-444-3333 | alice@example.com |
| 5 | Smith | NULL | smith@example.com |

@ -0,0 +1,53 @@
---
title: Updating Bookstore
description: Practice updating multiple rows with different conditions
order: 120
type: challenge
setup: |
```sql
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title VARCHAR(100),
category VARCHAR(50),
price DECIMAL(10,2),
stock INTEGER
);
INSERT INTO books (id, title, category, price, stock) VALUES
(1, 'The Great Gatsby', 'Fiction', 24.99, 15),
(2, 'SQL Basics', 'Technical', 39.99, 8),
(3, '1984', 'Fiction', 19.99, 12),
(4, 'Poetry Collection', 'Poetry', 14.99, 50),
(5, 'Database Design', 'Technical', 44.99, 20);
```
---
Given the following `books` table:
| id | title | category | price | stock |
| --- | ----------------- | --------- | ----- | ----- |
| 1 | The Great Gatsby | Fiction | 24.99 | 15 |
| 2 | SQL Basics | Technical | 39.99 | 8 |
| 3 | 1984 | Fiction | 19.99 | 12 |
| 4 | Poetry Collection | Poetry | 14.99 | 50 |
| 5 | Database Design | Technical | 44.99 | 20 |
Write the following update statements to make updates to the table:
1. Write a query to apply a 25% discount to all `Technical` books (multiply the price by 0.75)
2. Write another query to add "(Sale Edition)" to the titles of books that cost more than $25 after the previous update.
3. Write a final query to increase the stock of books with less than 10 in stock by 5.
## Expected Results
After your updates, the table should look like this:
| id | title | category | price | stock |
| --- | ----------------------------- | --------- | ----- | ----- |
| 1 | The Great Gatsby | Fiction | 24.99 | 15 |
| 3 | 1984 | Fiction | 19.99 | 12 |
| 4 | Poetry Collection | Poetry | 14.99 | 50 |
| 5 | Database Design(Sale Edition) | Technical | 33.74 | 20 |
| 2 | SQL Basics(Sale Edition) | Technical | 29.99 | 13 |
> You can run multiple UPDATE statements in a single query. Just separate them with a semicolon.
Loading…
Cancel
Save