Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions 2-exercises/task.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ In this homework, you are going to work with an ecommerce database. In this data
Below you will find a set of tasks for you to complete to set up a database for an e-commerce app.

To submit this homework write the correct commands for each question here:

```sql


Expand DownExpand Up@@ -35,16 +36,92 @@ Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a pie
Once you understand the database that you are going to work with, solve the following challenge by writing SQL queries using everything you learned about SQL:

1. Retrieve all the customers' names and addresses who live in the United States

```sql
ANSWER: SELECT name, address FROM customers WHERE country = 'United States';

```

2. Retrieve all the customers in ascending name sequence

```sql
ANSWER: SELECT * FROM customers ORDER BY name ASC;

```

3. Retrieve all the products whose name contains the word `socks`

```sql
ANSWER: SELECT * FROM products WHERE product_name LIKE '%socks%';

```

4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id.

```sql
ANSWER: SELECT pa.prod_id, p.product_name, pa.unit_price, pa.supp_id FROM products p Join product_availability pa ON (pa.prod_id = p.id) WHERE pa.unit_price > 100;

```

5. Retrieve the 5 most expensive products

```sql
ANSWER: SELECT * FROM products INNER JOIN product_availability ON prod_id = products.id ORDER BY unit_price DESC LIMIT 5;

```

6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name`

```sql
ANSWER: SELECT p.product_name, pa.unit_price, s.supplier_name FROM products p INNER JOIN product_availability pa ON (pa.prod_id = p.id) INNER JOIN suppliers s ON (s.id = pa.supp_id);

```

7. Retrieve all the products sold by suppliers based in the United Kingdom. The result should only contain the columns `product_name` and `supplier_name`.
8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity * unit price).

```sql
ANSWER: SELECT p.product_name, s.supplier_name FROM products p JOIN product_availability pa ON (pa.prod_id = p.id) JOIN suppliers s ON (s.id = pa.supp_id) WHERE s.country = 'United Kingdom';

```

8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity \* unit price).

```sql
ANSWER: SELECT oi.order_id, o.order_reference, o.order_date, oi.quantity * pa.unit_price AS total_cost FROM orders o JOIN order_items oi ON (o.id = oi.order_id) JOIN product_availability pa ON (oi.id = pa.prod_id) WHERE o.customer_id = 1;

```

9. Retrieve all orders, including order items, from customer named `Hope Crosby`

```sql
ANSWER: SELECT o.*, oi.* FROM orders o JOIN order_items oi ON (o.id = oi.order_id) JOIN customers c ON (o.customer_id = c.id) WHERE c.name = 'Hope Crosby';

```

10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`.

```sql
ANSWER: SELECT p.product_name, pa.unit_price, oi.quantity FROM products p JOIN product_availability pa ON (pa.prod_id = p.id) JOIN order_items oi ON (pa.prod_id = oi.product_id) JOIN orders o ON (o.id = oi.order_id) WHERE o.order_reference = 'ORD006';

```

11. Retrieve all the products with their supplier for all orders of all customers. The result should only contain the columns `name` (from customer), `order_reference`, `order_date`, `product_name`, `supplier_name` and `quantity`.

```sql
ANSWER: SELECT c.name, o.order_reference, o.order_date, p.product_name, s.supplier_name, oi.quantity FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) JOIN suppliers s ON (oi.supplier_id = s.id) JOIN products p ON (p.id = oi.product_id);

```

12. Retrieve the names of all customers who bought a product from a supplier based in China.
13. List all orders giving customer name, order reference, order date and order total amount (quantity * unit price) in descending order of total.

```sql
ANSWER: SELECT DISTINCT c.name FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (oi.order_id = o.id) JOIN products p ON (oi.product_id = p.id) JOIN suppliers s ON (oi.supplier_id = s.id) WHERE s.country = 'China';

```

13. List all orders giving customer name, order reference, order date and order total amount (quantity \* unit price) in descending order of total.

```sql
ANSWER: SELECT c.name, o.order_reference, o.order_date, oi.quantity * pa.unit_price AS total_amount FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (oi.order_id = o.id) JOIN product_availability pa ON (oi.product_id = pa.prod_id) ORDER BY total_amount DESC;

```
1 change: 1 addition & 0 deletions 3-projects/.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
node_modules
Loading