From 119a6a17edbdd144d7f4a6a0cb48e7a5c72f2d7f Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sat, 11 Feb 2023 12:51:12 +0000 Subject: [PATCH 01/10] Task 1 and 2 --- 2-exercises/task.md | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/2-exercises/task.md b/2-exercises/task.md index c48d2286..744af188 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -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 @@ -35,16 +36,34 @@ 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 \* FROM customers WHERE country ='United States'; + +id | name | address | city | country +----+--------------+----------------------------+------------------+--------------- +4 | Amber Tran | 6967 Ac Road | Villafranca Asti | United States +5 | Edan Higgins | Ap #840-3255 Tincidunt St. | Arles | United States + 2. Retrieve all the customers in ascending name sequence + +SELECT \* FROM customers ORDER BY name; +id | name | address | city | country +----+--------------------+-----------------------------+------------------+---------------- +4 | Amber Tran | 6967 Ac Road | Villafranca Asti | United States +3 | Britanney Kirkland | P.O. Box 577, 5601 Sem, St. | Little Rock | United Kingdom +5 | Edan Higgins | Ap #840-3255 Tincidunt St. | Arles | United States +1 | Guy Crawford | 770-2839 Ligula Road | Paris | France +2 | Hope Crosby | P.O. Box 276, 4976 Sit Rd. | Steyr | United Kingdom +6 | Quintessa Austin | 597-2737 Nunc Rd. | Saint-Marc | United Kingdom + 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. 5. Retrieve the 5 most expensive products 6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name` 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). +8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity \* unit price). 9. Retrieve all orders, including order items, from customer named `Hope Crosby` 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. 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. - +13. List all orders giving customer name, order reference, order date and order total amount (quantity \* unit price) in descending order of total. From 0bf0baa87b7df706dcb446c2629c9332ba31de3d Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sat, 11 Feb 2023 15:57:07 +0000 Subject: [PATCH 02/10] TASK 3- 6 --- 2-exercises/task.md | 67 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/2-exercises/task.md b/2-exercises/task.md index 744af188..d2bc973a 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -37,7 +37,7 @@ Once you understand the database that you are going to work with, solve the foll 1. Retrieve all the customers' names and addresses who live in the United States -SELECT \* FROM customers WHERE country ='United States'; +SELECT * FROM customers WHERE country ='United States'; id | name | address | city | country ----+--------------+----------------------------+------------------+--------------- @@ -46,7 +46,7 @@ id | name | address | city | country 2. Retrieve all the customers in ascending name sequence -SELECT \* FROM customers ORDER BY name; +SELECT * FROM customers ORDER BY name; id | name | address | city | country ----+--------------------+-----------------------------+------------------+---------------- 4 | Amber Tran | 6967 Ac Road | Villafranca Asti | United States @@ -57,9 +57,72 @@ id | name | address | city | country 6 | Quintessa Austin | 597-2737 Nunc Rd. | Saint-Marc | United Kingdom 3. Retrieve all the products whose name contains the word `socks` + +SELECT * FROM products WHERE product_name ILIKE '%socks%'; +id | product_name +----+------------------ +4 | Super warm socks +(1 row) + 4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id. + +SELECT products.id,products.product_name, product_availability.supp_id,product_availability.unit_price FROM products +cyf_ecommerce-# INNER JOIN product_availability ON product_availability.prod_id=products.id WHERE product_availability.unit_price >100; +id | product_name | supp_id | unit_price +----+----------------+---------+------------ +1 | Mobile Phone X | 4 | 249 +1 | Mobile Phone X | 1 | 299 +(2 rows) + 5. Retrieve the 5 most expensive products + +SELECT products.product_name, product_availability.unit_price FROM products +INNER JOIN product_availability ON product_availability.prod_id=products.id +ORDER BY unit_price DESC LIMIT 5; + +product_name | unit_price +-----------------+------------ +Mobile Phone X | 299 +Mobile Phone X | 249 +Javascript Book | 41 +Javascript Book | 40 +Javascript Book | 39 +(5 rows) + 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 +INNER JOIN product_availability ON product_availability.prod_id=products.id; + +INNER JOIN suppliers ON product_availability.supp_id=suppliers.id; + product_name | unit_price | supplier_name +-------------------------+------------+--------------- + Mobile Phone X | 249 | Sainsburys + Mobile Phone X | 299 | Amazon + Javascript Book | 41 | Taobao + Javascript Book | 39 | Argos + Javascript Book | 40 | Amazon + Le Petit Prince | 10 | Sainsburys + Le Petit Prince | 10 | Amazon + Super warm socks | 10 | Sainsburys + Super warm socks | 8 | Argos + Super warm socks | 5 | Taobao + Super warm socks | 10 | Amazon + Coffee Cup | 5 | Sainsburys + Coffee Cup | 4 | Argos + Coffee Cup | 4 | Taobao + Coffee Cup | 3 | Amazon + Ball | 20 | Taobao + Ball | 15 | Sainsburys + Ball | 14 | Amazon + Tee Shirt Olympic Games | 21 | Argos + Tee Shirt Olympic Games | 18 | Taobao + Tee Shirt Olympic Games | 20 | Amazon + + + + + 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). 9. Retrieve all orders, including order items, from customer named `Hope Crosby` From 64ff251fed2177d3734d5c7ca2ec588a1d356360 Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sat, 11 Feb 2023 20:44:38 +0000 Subject: [PATCH 03/10] TASKS 7 - 9 --- 2-exercises/task.md | 72 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/2-exercises/task.md b/2-exercises/task.md index d2bc973a..3c1141b8 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -37,7 +37,7 @@ Once you understand the database that you are going to work with, solve the foll 1. Retrieve all the customers' names and addresses who live in the United States -SELECT * FROM customers WHERE country ='United States'; +SELECT \* FROM customers WHERE country ='United States'; id | name | address | city | country ----+--------------+----------------------------+------------------+--------------- @@ -46,7 +46,7 @@ id | name | address | city | country 2. Retrieve all the customers in ascending name sequence -SELECT * FROM customers ORDER BY name; +SELECT \* FROM customers ORDER BY name; id | name | address | city | country ----+--------------------+-----------------------------+------------------+---------------- 4 | Amber Tran | 6967 Ac Road | Villafranca Asti | United States @@ -58,7 +58,7 @@ id | name | address | city | country 3. Retrieve all the products whose name contains the word `socks` -SELECT * FROM products WHERE product_name ILIKE '%socks%'; +SELECT \* FROM products WHERE product_name ILIKE '%socks%'; id | product_name ----+------------------ 4 | Super warm socks @@ -92,9 +92,9 @@ Javascript Book | 39 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 -INNER JOIN product_availability ON product_availability.prod_id=products.id; +INNER JOIN product_availability ON product_availability.prod_id=products.id +INNER JOIN suppliers ON product_availability.supp_id=suppliers.id; -INNER JOIN suppliers ON product_availability.supp_id=suppliers.id; product_name | unit_price | supplier_name -------------------------+------------+--------------- Mobile Phone X | 249 | Sainsburys @@ -118,14 +118,72 @@ INNER JOIN suppliers ON product_availability.supp_id=suppliers.id; Tee Shirt Olympic Games | 21 | Argos Tee Shirt Olympic Games | 18 | Taobao Tee Shirt Olympic Games | 20 | Amazon +(21 rows) + +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,product_availability.unit_price, suppliers.supplier_name, suppliers.country FROM products +INNER JOIN product_availability ON product_availability.prod_id=products.id +INNER JOIN suppliers ON product_availability.supp_id=suppliers.id WHERE suppliers.country='United Kingdom'; + + product_name | unit_price | supplier_name | country +-------------------------+------------+---------------+---------------- + Javascript Book | 39 | Argos | United Kingdom + Super warm socks | 8 | Argos | United Kingdom + Coffee Cup | 4 | Argos | United Kingdom + Tee Shirt Olympic Games | 21 | Argos | United Kingdom + Mobile Phone X | 249 | Sainsburys | United Kingdom + Le Petit Prince | 10 | Sainsburys | United Kingdom + Super warm socks | 10 | Sainsburys | United Kingdom + Coffee Cup | 5 | Sainsburys | United Kingdom + Ball | 15 | Sainsburys | United Kingdom +(9 rows) +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.customer_id, order_items.order_id, orders.order_reference, orders.order_date, order_items.quantity*product_availability.unit_price as "Total Cost" FROM order_items +INNER JOIN orders 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; + + customer_id | order_id | order_reference | order_date | Total Cost +-------------+----------+-----------------+------------+------------ + 1 | 1 | ORD001 | 2019-06-01 | 20 + 1 | 1 | ORD001 | 2019-06-01 | 18 + 1 | 1 | ORD001 | 2019-06-01 | 21 + 1 | 1 | ORD001 | 2019-06-01 | 50 + 1 | 1 | ORD001 | 2019-06-01 | 25 + 1 | 1 | ORD001 | 2019-06-01 | 40 + 1 | 1 | ORD001 | 2019-06-01 | 50 + 1 | 2 | ORD002 | 2019-07-15 | 40 + 1 | 2 | ORD002 | 2019-07-15 | 20 + 1 | 2 | ORD002 | 2019-07-15 | 32 + 1 | 2 | ORD002 | 2019-07-15 | 40 + 1 | 2 | ORD002 | 2019-07-15 | 10 + 1 | 2 | ORD002 | 2019-07-15 | 10 + 1 | 3 | ORD003 | 2019-07-11 | 30 + 1 | 3 | ORD003 | 2019-07-11 | 40 + 1 | 3 | ORD003 | 2019-07-11 | 40 + 1 | 3 | ORD003 | 2019-07-11 | 50 + 1 | 3 | ORD003 | 2019-07-11 | 28 + 1 | 3 | ORD003 | 2019-07-11 | 40 + 1 | 3 | ORD003 | 2019-07-11 | 30 +(20 rows) -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). 9. Retrieve all orders, including order items, from customer named `Hope Crosby` + +SELECT * from orders +INNER JOIN customers ON customers.id=orders.customer_id AND customers.name = 'Hope Crosby'; + + + id | order_date | order_reference | customer_id | id | name | address | city | country +----+------------+-----------------+-------------+----+-------------+----------------------------+-------+---------------- + 4 | 2019-05-24 | ORD004 | 2 | 2 | Hope Crosby | P.O. Box 276, 4976 Sit Rd. | Steyr | United Kingdom +(1 row) + + + 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. 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. From 42db3bffb3b7d497a4f90f1e2e88d47157ae6c77 Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sat, 11 Feb 2023 21:53:52 +0000 Subject: [PATCH 04/10] TASK 10 --- 2-exercises/task.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/2-exercises/task.md b/2-exercises/task.md index 3c1141b8..9fdfeb89 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -185,6 +185,35 @@ INNER JOIN customers ON customers.id=orders.customer_id AND customers.name = 'Ho 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. + +-->>> I just liked to add total column , and order by the total price the bigger amount on top! + +SELECT products.product_name, orders.order_reference, product_availability.unit_price,order_items.quantity, order_items.quantity * product_availability.unit_price AS "Total" FROM products +INNER JOIN product_availability On product_availability.prod_id=products.id +INNER JOIN order_items ON product_availability.prod_id=order_items.product_id +INNER JOIN orders ON order_items.order_id=orders.id WHERE orders.order_reference = 'ORD006' ORDER BY "Total" DESC; + + + + product_name | order_reference | unit_price | quantity | Total +------------------+-----------------+------------+----------+------- + Javascript Book | ORD006 | 41 | 1 | 41 + Javascript Book | ORD006 | 40 | 1 | 40 + Javascript Book | ORD006 | 39 | 1 | 39 + Super warm socks | ORD006 | 10 | 3 | 30 + Super warm socks | ORD006 | 10 | 3 | 30 + Super warm socks | ORD006 | 8 | 3 | 24 + Super warm socks | ORD006 | 5 | 3 | 15 + Coffee Cup | ORD006 | 5 | 3 | 15 + Coffee Cup | ORD006 | 4 | 3 | 12 + Coffee Cup | ORD006 | 4 | 3 | 12 + Le Petit Prince | ORD006 | 10 | 1 | 10 + Le Petit Prince | ORD006 | 10 | 1 | 10 + Coffee Cup | ORD006 | 3 | 3 | 9 +(13 rows) + 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. From bf7a7e57c524f7660fc478e0fdae9baa207610e5 Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sun, 12 Feb 2023 20:59:44 +0000 Subject: [PATCH 05/10] TASKS 11-13 --- 2-exercises/task.md | 125 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/2-exercises/task.md b/2-exercises/task.md index 9fdfeb89..df898ddc 100644 --- a/2-exercises/task.md +++ b/2-exercises/task.md @@ -193,8 +193,6 @@ INNER JOIN product_availability On product_availability.prod_id=products.id INNER JOIN order_items ON product_availability.prod_id=order_items.product_id INNER JOIN orders ON order_items.order_id=orders.id WHERE orders.order_reference = 'ORD006' ORDER BY "Total" DESC; - - product_name | order_reference | unit_price | quantity | Total ------------------+-----------------+------------+----------+------- Javascript Book | ORD006 | 41 | 1 | 41 @@ -212,8 +210,131 @@ INNER JOIN orders ON order_items.order_id=orders.id WHERE orders.order_reference Coffee Cup | ORD006 | 3 | 3 | 9 (13 rows) + 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 +JOIN orders ON orders.customer_id=customers.id +JOIN order_items ON order_items.order_id=orders.id +JOIN products ON order_items.product_id=products.id +JOIN suppliers ON suppliers.id=order_items.supplier_id; + + + name | order_reference | order_date | product_name | supplier_name | quantity +--------------------+-----------------+------------+-------------------------+---------------+---------- + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | Taobao | 1 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | Taobao | 5 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | Argos | 4 + Guy Crawford | ORD002 | 2019-07-15 | Le Petit Prince | Sainsburys | 1 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | Argos | 10 + Guy Crawford | ORD003 | 2019-07-11 | Ball | Taobao | 2 + Hope Crosby | ORD004 | 2019-05-24 | Mobile Phone X | Amazon | 1 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | Argos | 2 + Britanney Kirkland | ORD005 | 2019-05-30 | Le Petit Prince | Amazon | 1 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | Taobao | 3 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | Taobao | 1 + Amber Tran | ORD006 | 2019-07-05 | Le Petit Prince | Sainsburys | 1 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | Sainsburys | 3 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | Argos | 15 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | Amazon | 1 + Edan Higgins | ORD008 | 2019-07-23 | Mobile Phone X | Sainsburys | 1 + Edan Higgins | ORD009 | 2019-07-24 | Ball | Sainsburys | 2 + Edan Higgins | ORD010 | 2019-05-10 | Ball | Taobao | 1 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | Amazon | 5 +(19 rows) + + 12. Retrieve the names of all customers who bought a product from a supplier based in China. +SELECT customers.name, suppliers.supplier_name,suppliers.country FROM customers +JOIN orders ON orders.customer_id=customers.id +JOIN order_items ON order_items.order_id=orders.id +JOIN suppliers ON order_items.supplier_id=suppliers.id +WHERE suppliers.country='China'; + + name | supplier_name | country +--------------+---------------+--------- + Guy Crawford | Taobao | China + Guy Crawford | Taobao | China + Guy Crawford | Taobao | China + Amber Tran | Taobao | China + Amber Tran | Taobao | China + Edan Higgins | Taobao | China +(6 rows) + + 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, products.product_name, +order_items.quantity, product_availability.unit_price, +(order_items.quantity * product_availability.unit_price) AS "Total Amount" +FROM customers +JOIN orders ON orders.customer_id=customers.id +JOIN order_items ON order_items.order_id=orders.id +JOIN products ON order_items.product_id=products.id +JOIN product_availability ON products.id=product_availability.prod_id; + + + name | order_reference | order_date | product_name | quantity | unit_price | Total Amount +--------------------+-----------------+------------+-------------------------+----------+------------+-------------- + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | 1 | 20 | 20 + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | 1 | 18 | 18 + Guy Crawford | ORD001 | 2019-06-01 | Tee Shirt Olympic Games | 1 | 21 | 21 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 10 | 50 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 5 | 25 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 8 | 40 + Guy Crawford | ORD001 | 2019-06-01 | Super warm socks | 5 | 10 | 50 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 10 | 40 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 5 | 20 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 8 | 32 + Guy Crawford | ORD002 | 2019-07-15 | Super warm socks | 4 | 10 | 40 + Guy Crawford | ORD002 | 2019-07-15 | Le Petit Prince | 1 | 10 | 10 + Guy Crawford | ORD002 | 2019-07-15 | Le Petit Prince | 1 | 10 | 10 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 3 | 30 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 4 | 40 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 4 | 40 + Guy Crawford | ORD003 | 2019-07-11 | Coffee Cup | 10 | 5 | 50 + Guy Crawford | ORD003 | 2019-07-11 | Ball | 2 | 14 | 28 + Guy Crawford | ORD003 | 2019-07-11 | Ball | 2 | 15 | 30 + Guy Crawford | ORD003 | 2019-07-11 | Ball | 2 | 20 | 40 + Hope Crosby | ORD004 | 2019-05-24 | Mobile Phone X | 1 | 299 | 299 + Hope Crosby | ORD004 | 2019-05-24 | Mobile Phone X | 1 | 249 | 249 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | 2 | 40 | 80 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | 2 | 39 | 78 + Britanney Kirkland | ORD005 | 2019-05-30 | Javascript Book | 2 | 41 | 82 + Britanney Kirkland | ORD005 | 2019-05-30 | Le Petit Prince | 1 | 10 | 10 + Britanney Kirkland | ORD005 | 2019-05-30 | Le Petit Prince | 1 | 10 | 10 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 3 | 9 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 4 | 12 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 4 | 12 + Amber Tran | ORD006 | 2019-07-05 | Coffee Cup | 3 | 5 | 15 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | 1 | 40 | 40 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | 1 | 39 | 39 + Amber Tran | ORD006 | 2019-07-05 | Javascript Book | 1 | 41 | 41 + Amber Tran | ORD006 | 2019-07-05 | Le Petit Prince | 1 | 10 | 10 + Amber Tran | ORD006 | 2019-07-05 | Le Petit Prince | 1 | 10 | 10 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 10 | 30 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 5 | 15 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 8 | 24 + Amber Tran | ORD006 | 2019-07-05 | Super warm socks | 3 | 10 | 30 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 10 | 150 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 5 | 75 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 8 | 120 + Amber Tran | ORD007 | 2019-04-05 | Super warm socks | 15 | 10 | 150 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | 1 | 20 | 20 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | 1 | 18 | 18 + Edan Higgins | ORD008 | 2019-07-23 | Tee Shirt Olympic Games | 1 | 21 | 21 + Edan Higgins | ORD008 | 2019-07-23 | Mobile Phone X | 1 | 299 | 299 + Edan Higgins | ORD008 | 2019-07-23 | Mobile Phone X | 1 | 249 | 249 + Edan Higgins | ORD009 | 2019-07-24 | Ball | 2 | 14 | 28 + Edan Higgins | ORD009 | 2019-07-24 | Ball | 2 | 15 | 30 + Edan Higgins | ORD009 | 2019-07-24 | Ball | 2 | 20 | 40 + Edan Higgins | ORD010 | 2019-05-10 | Ball | 1 | 14 | 14 + Edan Higgins | ORD010 | 2019-05-10 | Ball | 1 | 15 | 15 + Edan Higgins | ORD010 | 2019-05-10 | Ball | 1 | 20 | 20 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 10 | 50 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 5 | 25 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 8 | 40 + Edan Higgins | ORD010 | 2019-05-10 | Super warm socks | 5 | 10 | 50 +(59 rows) \ No newline at end of file From 20f9fcc4c1cabeb0afeac65f30676ec0a0d479bc Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sun, 12 Feb 2023 22:40:24 +0000 Subject: [PATCH 06/10] Create yf-ecommerce-api --- 3-projects/server.js | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 3-projects/server.js diff --git a/3-projects/server.js b/3-projects/server.js new file mode 100644 index 00000000..4d71f35f --- /dev/null +++ b/3-projects/server.js @@ -0,0 +1,57 @@ +const express = require("express"); +const app = express(); +const port = 500; + +app.use(express.json()); + +const { Pool } = require(""); + +const pool = new Pool({ + user: "postgres", + host: "localhost", + database: "cyf_ecommerce", + password: "Kdagaal123", + port: 5432, +}); + +app.get("cyf-ecommerce-api", (req, res) => { + res.status(200).send(`Wellcom cyf-ecommerce-api + visit these roots; +/customers +/suppliers +/products + `); +}); + +app.get("cyf-ecommerce-api/customers", (req, res) => { + pool + .query("SELECT * FROM customers") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("cyf-ecommerce-api/suppliers", (req, res) => { + pool + .query("SELECT * FROM suppliers") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("cyf-ecommerce-api/products", (req, res) => { + pool + .query("SELECT * FROM products") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); +app.listen(port, () => { + console.log(`server is running ${port}`); +}); From e8269ea104366b72e5ad5b63d8db8f88f0ab426f Mon Sep 17 00:00:00 2001 From: khmdagal Date: Sun, 12 Feb 2023 23:12:15 +0000 Subject: [PATCH 07/10] test --- .gitignore | 1 + 3-projects/server.js | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/3-projects/server.js b/3-projects/server.js index 4d71f35f..de89ab62 100644 --- a/3-projects/server.js +++ b/3-projects/server.js @@ -24,13 +24,14 @@ app.get("cyf-ecommerce-api", (req, res) => { }); app.get("cyf-ecommerce-api/customers", (req, res) => { - pool - .query("SELECT * FROM customers") - .then((data) => res.json(data.rows)) - .catch((err) => { - console.error(err); - res.status(500).json(err); - }); + // pool + // .query("SELECT * FROM customers") + // .then((data) => res.json(data.rows)) + // .catch((err) => { + // console.error(err); + // res.status(500).json(err); + // }); + res.send('hello') }); app.get("cyf-ecommerce-api/suppliers", (req, res) => { From 8b9f9ab8bc3520b49d6be0038bbd9db557f496e8 Mon Sep 17 00:00:00 2001 From: khmdagal Date: Mon, 13 Feb 2023 00:18:19 +0000 Subject: [PATCH 08/10] Fix The / route --- 3-projects/server.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/3-projects/server.js b/3-projects/server.js index de89ab62..60282024 100644 --- a/3-projects/server.js +++ b/3-projects/server.js @@ -1,10 +1,10 @@ const express = require("express"); const app = express(); -const port = 500; +const port = 5000; app.use(express.json()); -const { Pool } = require(""); +const { Pool } = require("pg"); const pool = new Pool({ user: "postgres", @@ -14,7 +14,8 @@ const pool = new Pool({ port: 5432, }); -app.get("cyf-ecommerce-api", (req, res) => { + +app.get("/cyf-ecommerce-api", (req, res) => { res.status(200).send(`Wellcom cyf-ecommerce-api visit these roots; /customers @@ -23,18 +24,17 @@ app.get("cyf-ecommerce-api", (req, res) => { `); }); -app.get("cyf-ecommerce-api/customers", (req, res) => { - // pool - // .query("SELECT * FROM customers") - // .then((data) => res.json(data.rows)) - // .catch((err) => { - // console.error(err); - // res.status(500).json(err); - // }); - res.send('hello') +app.get("/cyf-ecommerce-api/customers", (req, res) => { + pool + .query("SELECT * FROM customers") + .then((data) => res.json(data.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); }); -app.get("cyf-ecommerce-api/suppliers", (req, res) => { +app.get("/cyf-ecommerce-api/suppliers", (req, res) => { pool .query("SELECT * FROM suppliers") .then((data) => res.json(data.rows)) @@ -44,7 +44,7 @@ app.get("cyf-ecommerce-api/suppliers", (req, res) => { }); }); -app.get("cyf-ecommerce-api/products", (req, res) => { +app.get("/cyf-ecommerce-api/products", (req, res) => { pool .query("SELECT * FROM products") .then((data) => res.json(data.rows)) @@ -53,6 +53,8 @@ app.get("cyf-ecommerce-api/products", (req, res) => { res.status(500).json(err); }); }); + app.listen(port, () => { console.log(`server is running ${port}`); }); + From 94cd58c062e3742f885a9bfd9220e4dab7695de0 Mon Sep 17 00:00:00 2001 From: khmdagal Date: Mon, 13 Feb 2023 00:49:47 +0000 Subject: [PATCH 09/10] STRETCH GOAL --- 3-projects/server.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/3-projects/server.js b/3-projects/server.js index 60282024..926be767 100644 --- a/3-projects/server.js +++ b/3-projects/server.js @@ -14,6 +14,10 @@ const pool = new Pool({ port: 5432, }); +const productsBySuppliers = `SELECT products.product_name, suppliers.supplier_name, product_availability.unit_price FROM products +JOIN product_availability on product_availability.prod_id=products.id +JOIN suppliers on product_availability.supp_id=suppliers.id +ORDER BY supplier_name`; app.get("/cyf-ecommerce-api", (req, res) => { res.status(200).send(`Wellcom cyf-ecommerce-api @@ -54,7 +58,15 @@ app.get("/cyf-ecommerce-api/products", (req, res) => { }); }); +app.get("/cyf-ecommerce-api/productsBySuppliers", (req, res) => { + pool + .query(`${productsBySuppliers}`) + .then((data) => res.json(data)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); app.listen(port, () => { console.log(`server is running ${port}`); }); - From fc3456c2f021d61f45fc7f7e655ddcdf1a52e1d8 Mon Sep 17 00:00:00 2001 From: khmdagal Date: Thu, 16 Feb 2023 22:00:26 +0000 Subject: [PATCH 10/10] SQL Week 3 --- .gitignore | 3 +- 3-projects/server.js | 150 +++++++++++++++++++++++++++++++++++++++ 3-projects/taskSQL-W3.md | 33 +++++++++ 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 3-projects/taskSQL-W3.md diff --git a/.gitignore b/.gitignore index b512c09d..b36f88aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +.vscode/settings.json diff --git a/3-projects/server.js b/3-projects/server.js index 926be767..b3b86a5b 100644 --- a/3-projects/server.js +++ b/3-projects/server.js @@ -1,3 +1,4 @@ +const { query } = require("express"); const express = require("express"); const app = express(); const port = 5000; @@ -67,6 +68,155 @@ app.get("/cyf-ecommerce-api/productsBySuppliers", (req, res) => { res.status(500).json(err); }); }); + +//------------- FROM HERE IS SQL WEEK 3 ------------------------ + +app.get("/customers/:customerId", (req, res) => { + const customerId = +req.params.customerId; + + pool + .query( + `SELECT c.name, c.address, c.city, c.country FROM customers c WHERE id=${customerId}` + ) + .then((customer) => res.json(customer.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get("/product_avaialability", (req, res) => { + pool + .query(`SELECT * FROM product_availability;`) + .then((availableProducts) => res.json(availableProducts.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.post("/addNewCustomer", (req, res) => { + pool + .query( + ` + INSERT INTO customers (name,address,city,country) + VALUES ('New name3','New address3', 'XYZ City3','ABC Country3'); + ` + ) + .then(() => res.send("A new customer is added")) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.post("/addNewProduct", (req, res) => { + pool + .query(`INSERT INTO products (product_name) VALUES ('Book');`) + .then(() => res.send("New row is added")) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.post("/addProductAvailability", (req, res) => { + pool + .query( + `INSERT INTO product_availability (prod_id,supp_id,unit_price) + VALUES (13,2,15);` + ) + .then(() => + res.status(200).json("new record is added to product_availability table") + ) + .catch((err) => { + console.error(err); + res.send(err); + }); +}); + +app.post("/customers/:customerId/AddNewOrders", (req, res) => { + const custID = +req.params.customerId; + const query = `INSERT INTO orders (order_date,order_reference,customer_id) + VALUES ('2023-01-10','0RD012',${Number(custID)})`; + + pool + .query(query) + .then(() => res.send("Anew order is added for customer " + custID)) + .catch((err) => { + console.error(err); + res.status(500).send(err); + }); +}); + +//Look this put block and it's fucntionality thing about update and put methods +app.put("/updateCustomerRecord/:customerId", (req, res) => { + const custID = +req.params.customerId; + console.log(custID, `${Number(custID)}`); + pool + .query( + ` +UPDATE customers +SET name='Ahmed',address='10 AliClose M82UY',city='Manchester',country='Canada' +WHERE id ='${custID}' +` + ) + .then(() => res.send(`customer id : ${custID} has been updated`)) + .catch((err) => { + console.error(err); + res.status(500).send(err); + }); +}); + +app.delete("/deleteOrder/:orderId", (req, res) => { + const orderId = +req.params.orderId; + pool + .query(`DELETE FROM orders WHERE orders.id='${Number(orderId)}'`) + .then(() => { + res.send("order number " + orderId + " has been deleted").catch((err) => { + console.error(err); + res.send(err); + }); + }); +}); + + + +app.delete("/deleteCustomer/:customerId", (req, res) => { + const customerId = +req.params.customerId; + + pool + .query( + `Delete FROM customers + JOIN orders ON orders.customer_id=customers.id + WHERE customers.id='${customerId} AND orders.customer_id='${customerId}'` + ) + .then((customer) => res.json(customer.rows)) + .catch((err) => { + console.error(err); + res.status(500).json(err); + }); +}); + +app.get(`/getAllCustomerOrders/:customerId/orders`, (req, res) => { + const custID = +req.params.customerId; + + pool + .query( + `SELECT c.name, o.order_reference,o.order_date, p.product_name, oi.quantity,s.supplier_name, pa.unit_price from customers c +JOIN orders o ON o.customer_id=c.id +JOIN order_items oi on oi.order_id=o.id +JOIN products p ON p.id=oi.product_id +JOIN suppliers s ON s.id=oi.supplier_id +JOIN product_availability pa ON pa.prod_id=oi.product_id +WHERE c.id = '${custID}'`) + .then((customerOrders) => res.json(customerOrders.rows)) + .catch((err) => { + console.error(err); + res.status(500).send(err); + }); +}); + app.listen(port, () => { console.log(`server is running ${port}`); }); diff --git a/3-projects/taskSQL-W3.md b/3-projects/taskSQL-W3.md new file mode 100644 index 00000000..1aef922c --- /dev/null +++ b/3-projects/taskSQL-W3.md @@ -0,0 +1,33 @@ +# Homework + +## Submission + +You can continue working on the code from last week for this weeks task. + +To submit you should open a pull request with all of your code in this folder. + +## Task + +In the following homework, you will create new API endpoints in the NodeJS application `cyf-ecommerce-api` that you created for last week's homework for the Database 2 class. + +- If you don't have it already, add a new GET endpoint `/products` to load all the product names along with their prices and supplier names. + +- Update the previous GET endpoint `/products` to filter the list of products by name using a query parameter, for example `/products?name=Cup`. This endpoint should still work even if you don't use the `name` query parameter! + +- Add a new GET endpoint `/customers/:customerId` to load a single customer by ID. ---------->>>>>>>>> DONE + +- Add a new POST endpoint `/customers` to create a new customer with name, address, city and country. ---------->>>>>>>>> DONE + +- Add a new POST endpoint `/products` to create a new product. + +- Add a new POST endpoint `/availability` to create a new product availability (with a price and a supplier id). Check that the price is a positive integer and that both the product and supplier ID's exist in the database, otherwise return an error. + +- Add a new POST endpoint `/customers/:customerId/orders` to create a new order (including an order date, and an order reference) for a customer. Check that the customerId corresponds to an existing customer or return an error. + +- Add a new PUT endpoint `/customers/:customerId` to update an existing customer (name, address, city and country). + +- Add a new DELETE endpoint `/orders/:orderId` to delete an existing order along with all the associated order items. + +- Add a new DELETE endpoint `/customers/:customerId` to delete an existing customer only if this customer doesn't have orders. + +- Add a new GET endpoint `/customers/:customerId/orders` to load all the orders along with the items in the orders of a specific customer. Especially, the following information should be returned: order references, order dates, product names, unit prices, suppliers and quantities.