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
64 changes: 64 additions & 0 deletions 2-exercises/task.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,16 +35,80 @@ 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

=> select name from customers where country = 'United Kingdom';

2. Retrieve all the customers in ascending name sequence

=> SELECT name FROM customers ORDER BY name ASC;

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

=> 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.

=> SELECT products.\*, product_availability.unit_price, product_availability.supp_id
FROM products, product_availability
WHERE product_availability.unit_price > 100;

5. Retrieve the 5 most expensive products

=> SELECT \* FROM products, product_availability ORDER BY product_availability.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`

=> SELECT products.product_name, product_availability.unit_price, suppliers.supplier_name
FROM products, product_availability, suppliers;

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, suppliers
WHERE suppliers.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).

=> SELECT orders.id, orders.order_reference , orders.order_date , (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 o.id, o.order_reference, o.order_date,customers.name FROM orders AS o INNER JOIN customers ON customers.id = o.customer_id INNER JOIN order_items AS oi ON oi.id = oi.order_id WHERE customers.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 order_items.order_id=orders.id AND order_reference='ORD006'
INNER JOIN products ON order_items.product_id=products.id
INNER JOIN product_availability ON order_items.supplier_id=product_availability.supp_id;

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

12. Retrieve the names of all customers who bought a product from a supplier based in China.

=> SELECT DISTINCT customers.name
FROM customers
INNER JOIN orders ON customers.id = orders.customer_id
INNER JOIN order_items ON orders.id = order_items.order_id
INNER JOIN products ON order_items.product_id = products.id
INNER JOIN suppliers ON order_items.supplier_id = suppliers.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, SUM(order_items.quantity \* product_availability.unit_price) AS total
FROM customers
INNER JOIN orders ON customers.id = orders.customer_id
INNER JOIN order_items ON orders.id = order_items.order_id
INNER JOIN product_availability ON order_items.product_id = product_availability.prod_id AND order_items.supplier_id = product_availability.supp_id
GROUP BY customers.name, orders.order_reference, orders.order_date
ORDER BY total DESC;

1 change: 1 addition & 0 deletions 3-projects/task.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ Add all your `javascript` files in this folder and create a pull request to subm
## Task

- Create a new NodeJS application called `cyf-ecommerce-api`

- Add Express and node-postgres and make sure you can start the server with `node server.js`
- Add a new GET endpoint `/customers` to return all the customers from the database
- Add a new GET endpoint `/suppliers` to return all the suppliers from the database
Expand Down