Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
Open
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
1 change: 0 additions & 1 deletion 2-exercises/cyf_ecommerce.sql
Original file line numberDiff line numberDiff line change
Expand Up@@ -121,4 +121,3 @@ INSERT INTO order_items VALUES (16, 8, 1, 4, 1);
INSERT INTO order_items VALUES (17, 9, 6, 4, 2);
INSERT INTO order_items VALUES (18, 10, 6, 2, 1);
INSERT INTO order_items VALUES (19, 10, 4, 1, 5);

76 changes: 74 additions & 2 deletions 2-exercises/task.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,78 @@ Below you will find a set of tasks for you to complete to set up a database for

To submit this homework write the correct commands for each question here:
```sql
1-SELECT name,address FROM customers WHERE country='United States';
2- SELECT * FROM customer ORDER BY name ASC ;
3-SELECT * FROM products WHERE product_name ILIKE '%socks%';
4-SELECT product_availability.prod_id,product_name,product_availability.unit_price, product_availability.supp_id
FROM products
INNER JOIN product_availability
ON product_availability.prod_id=products.id
WHERE unit_price>100
5-SELECT product_name FROM products
INNER JOIN product_availability
ON product_availability.prod_id=products.id
ORDER BY unit_price DESC
LIMIT 5;
6-SELECT DISTINCT products.product_name, product_availability.unit_price, suppliers.supplier_name
FROM products
INNER JOIN product_availability
ON product_availability.prod_id=products.id
INNER JOIN suppliers
ON product_availability.supp_id=suppliers.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`.
SELECT products.product_name,suppliers.supplier_name
FROM products
INNER JOIN product_availability
ON product_availability.prod_id=products.id
INNER JOIN suppliers
ON product_availability.supp_id=suppliers.id
WHERE country='United Kingdom';

8- Retrieve all orders, including order items, from customer ID Include order id, reference, date and total cost (calculated as quantity * unit price).
SELECT orders.id,orders.order_date,orders.order_reference, (order_items.quantity * product_availability.unit_price) AS total_cost
FROM orders
INNER JOIN order_items ON order_items.order_id=orders.id
INNER JOIN product_availability ON product_availability.prod_id=order_items.product_id
WHERE orders.customer_id = 1;

9- Retrieve all orders, including order items, from customer named `Hope Crosby`
SELECT * FROM customers
INNER JOIN orders ON customers.id=orders.customer_id
INNER JOIN order_items ON order_items.order_id=orders.id
WHERE 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.
SELECT products.product_name, product_availability.unit_price, order_items.quantity
FROM orders
INNER JOIN order_items ON orders.id = order_items.order_id
INNER JOIN products ON products.id = order_items.product_id
INNER JOIN product_availability ON product_availability.prod_id = order_items.product_id
WHERE orders.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.
SELECT customers.name,orders.order_reference,orders.order_date,products.product_name,suppliers.supplier_name,order_items.quantity FROM customers
INNER JOIN orders ON orders.customer_id =customers.id
INNER JOIN order_items ON orders.id = order_items.order_id
INNER JOIN products ON products.id = order_items.product_id
INNER JOIN suppliers ON suppliers.id =order_items.supplier_id;

12-Retrieve the names of all customers who bought a product from a supplier based in China.
SELECT customers.name
FROM suppliers
INNER JOIN order_items ON suppliers.id = order_items.supplier_id
INNER JOIN orders ON order_items.order_id = orders.id
INNER JOIN customers ON customers.id = orders.customer_id
WHERE suppliers.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.
SELECT customers.name,orders.order_reference,orders.order_date,(quantity * unit_price) AS total_amount
FROM customers
INNER JOIN orders ON orders.customer_id = customers.id
INNER JOIN order_items ON order_items.order_id = orders.id
INNER JOIN product_availability ON order_items.product_id = product_availability.prod_id
ORDER BY total_amount DESC;


```
Expand All@@ -28,13 +100,14 @@ Import the file [`cyf_ecommerce.sql`](./cyf_ecommerce.sql) in your newly created
psql -d cyf_ecommerce -f cyf_ecommerce.sql
```

Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a piece of paper and draw the database with the different relationships between tables (as defined by the REFERENCES keyword in the CREATE TABLE commands). Identify the foreign keys and make sure you understand the full database schema.
Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a piece of paper and draw the database with the different relationships between tables (as defined by the REFERENCES keyword in the CREATE TABLE commands). Identify the foreign keys and make sure you understand the full database schema

## Task

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

2. Retrieve all the customers in ascending name sequence
3. Retrieve all the products whose name contains the word `socks`
4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id.
Expand All@@ -47,4 +120,3 @@ Once you understand the database that you are going to work with, solve the foll
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`.
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.