From 0a6ad086a04055ba5069b8e9935e5efa3cef9cc1 Mon Sep 17 00:00:00 2001 From: bireworld Date: Mon, 16 Nov 2020 09:02:19 +0000 Subject: [PATCH 1/9] database and mentors table created --- week-1/mandatory/2-classes-db/cyf_classes.sql | 12 ++++++++++++ week-1/mandatory/2-classes-db/task.md | 14 ++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 week-1/mandatory/2-classes-db/cyf_classes.sql diff --git a/week-1/mandatory/2-classes-db/cyf_classes.sql b/week-1/mandatory/2-classes-db/cyf_classes.sql new file mode 100644 index 00000000..dae3e1dc --- /dev/null +++ b/week-1/mandatory/2-classes-db/cyf_classes.sql @@ -0,0 +1,12 @@ +drop table if exists mentors; +drop table if exists students; +drop table if exists customers; + +CREATE TABLE mentors ( + name VARCHAR(30) NOT NULL, + years_in_glascow INT NOT NULL, + address VARCHAR(120), + city VARCHAR(30), + postcode VARCHAR(30), + fav_prog_language VARCHAR(20) +); \ No newline at end of file diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index be8b7cf0..dbd3ec24 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -8,6 +8,20 @@ To submit this homework write the correct commands for each question here: ```sql +//CREATE DATABASE +createdb -p 5432 -U postgres cyf_classes + +//LOG IN TO DATABASE +psql cyf_classes + +//CREATE mentors TABLE + +CREATE TABLE mentors ( +id SERIAL PRIMARY KEY, +name VARCHAR(30) NOT NULL, +address VARCHAR(120), +years_in_Glascow INT NOT NULL, +fav_prog_language VARCHAR(30)); ``` From d5a8779f279a7d1e66f7ce4ca7ef8ba6584a3633 Mon Sep 17 00:00:00 2001 From: bireworld Date: Tue, 17 Nov 2020 00:17:56 +0000 Subject: [PATCH 2/9] mentors,students and classes tables created and Insertion of data --- week-1/mandatory/2-classes-db/task.md | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index dbd3ec24..95458c89 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -23,6 +23,68 @@ address VARCHAR(120), years_in_Glascow INT NOT NULL, fav_prog_language VARCHAR(30)); +// verifying mentors Table created + +\d mentors + +//inserting mentors + +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('James Brown', '125 Barker St, MK112AA',6,'Python'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Billi Jean', '5 George St,B12UF',11,'Javascript'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Sara Major', '13 Prince Rd,DL62GH',2,'Java'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Mark Twain', '133 Elzabeth Rd,GL11BD',23,'C++'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Mel Gibson', '12 Gibson St,GL23FK',13,'HTML'); + + +//verifying entries for mentors + SELECT * FROM mentors; + +//Create Table students + +CREATE TABLE students ( name VARCHAR(30) NOT NULL,address VARCHAR(120),CYF_graduate VARCHAR(30)); + +//verifying Students Table created + +\d students + +// Inserting students + +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('kanye west','56 Rood End Rood, B665FG ','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('kim Lee','56 Moat Road, B562Gl ','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Brian McNight','20 Cross Street, WS110BZ ','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Sam Cook','29 Briage Street, WS110DQ ','yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Herman Cooper','97 Loxley Road, CV359JY ','yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('North Hampton','1 Crispin Street, NN12JH','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Barton Smith','87 Finchfiels Road, WV39LQ','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Simone Malanga','56 Ashworth Road, WS115DS','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Bobby Brown','51 Overdale Road, TF34BX','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Mathew Perry','10 Mathew Street, L34AA','Yes'); + +//verifying entries for students + + SELECT * FROM students; + +//classes Table created + +CREATE TABLE classes(mentor_name VARCHAR(30), module VARCHAR(30),course_date DATE NOT NULL, course_location VARCHAR(30)); + +//Verify classes table created + +\d classes + +//Insert entries to classes table + + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Rody Kirwan','React', '2019-10-01','West Midland'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Jason Sancho','Javascript', '2019-11-05','West Midland'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Andrew Jackson','Node', '2019-07-02','London'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Stella Markov','HTML', '2019-02-07','Manchester'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Sandeep Singh','CSS', '2019-03-09','Glascow'); + +// verify data Entries to classes + + select * from classes; + + ``` When you have finished all of the questions - open a pull request with your answers to the `Databases-Homework` repository. From 31f8afa8f9a9a0d3d992d05736fa8dcf1ccdf99d Mon Sep 17 00:00:00 2001 From: bireworld Date: Thu, 19 Nov 2020 08:22:56 +0000 Subject: [PATCH 3/9] classes taken by students complete --- week-1/mandatory/2-classes-db/cyf_classes.sql | 61 +++++++++++++++---- week-1/mandatory/2-classes-db/task.md | 32 ++++++---- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/week-1/mandatory/2-classes-db/cyf_classes.sql b/week-1/mandatory/2-classes-db/cyf_classes.sql index dae3e1dc..c23103e0 100644 --- a/week-1/mandatory/2-classes-db/cyf_classes.sql +++ b/week-1/mandatory/2-classes-db/cyf_classes.sql @@ -1,12 +1,51 @@ -drop table if exists mentors; -drop table if exists students; -drop table if exists customers; - CREATE TABLE mentors ( - name VARCHAR(30) NOT NULL, - years_in_glascow INT NOT NULL, - address VARCHAR(120), - city VARCHAR(30), - postcode VARCHAR(30), - fav_prog_language VARCHAR(20) -); \ No newline at end of file +id SERIAL PRIMARY KEY, +name VARCHAR(30) NOT NULL, +address VARCHAR(120), +years_in_Glascow INT NOT NULL, +fav_prog_language VARCHAR(30)); + + + +CREATE TABLE classes( + id SERIAL PRIMARY KEY, + mentor_name VARCHAR(30), + module VARCHAR(30), + course_date DATE NOT NULL, + course_location VARCHAR(30)); + + +CREATE TABLE students ( + id SERIAL PRIMARY KEY, + name VARCHAR(30) NOT NULL, + address VARCHAR(120), + CYF_graduate VARCHAR(30), + classes_id INT REFERENCES classes (id)); + + + +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('James Brown', '125 Barker St, MK112AA',6,'Python'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Billi Jean', '5 George St,B12UF',11,'Javascript'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Sara Major', '13 Prince Rd,DL62GH',2,'Java'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Mark Twain', '133 Elzabeth Rd,GL11BD',23,'C++'); +INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ('Mel Gibson', '12 Gibson St,GL23FK',13,'HTML'); + + +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('kanye west','56 Rood End Rood, B665FG ','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('kim Lee','56 Moat Road, B562Gl ','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Brian McNight','20 Cross Street, WS110BZ ','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Sam Cook','29 Briage Street, WS110DQ ','yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Herman Cooper','97 Loxley Road, CV359JY ','yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('North Hampton','1 Crispin Street, NN12JH','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Barton Smith','87 Finchfiels Road, WV39LQ','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Simone Malanga','56 Ashworth Road, WS115DS','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Bobby Brown','51 Overdale Road, TF34BX','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Mathew Perry','10 Mathew Street, L34AA','Yes'); + + + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Rody Kirwan','React', '2019-10-01','West Midland'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Jason Sancho','Javascript', '2019-11-05','West Midland'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Andrew Jackson','Node', '2019-07-02','London'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Stella Markov','HTML', '2019-02-07','Manchester'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Sandeep Singh','CSS', '2019-03-09','Glascow'); + diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index 95458c89..9dc5e4ba 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -8,7 +8,7 @@ To submit this homework write the correct commands for each question here: ```sql -//CREATE DATABASE +//CREATE DATABASE createdb -p 5432 -U postgres cyf_classes //LOG IN TO DATABASE @@ -16,7 +16,7 @@ psql cyf_classes //CREATE mentors TABLE -CREATE TABLE mentors ( +CREATE TABLE mentors ( id SERIAL PRIMARY KEY, name VARCHAR(30) NOT NULL, address VARCHAR(120), @@ -41,7 +41,7 @@ INSERT INTO mentors (name, address, years_in_Glascow,fav_prog_language) VALUES ( //Create Table students -CREATE TABLE students ( name VARCHAR(30) NOT NULL,address VARCHAR(120),CYF_graduate VARCHAR(30)); +CREATE TABLE students (id SERIAL PRIMARY KEY, name VARCHAR(30) NOT NULL,address VARCHAR(120),CYF_graduate VARCHAR(30)); //verifying Students Table created @@ -54,29 +54,29 @@ INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('kim Lee','56 Moat Road INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Brian McNight','20 Cross Street, WS110BZ ','No'); INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Sam Cook','29 Briage Street, WS110DQ ','yes'); INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Herman Cooper','97 Loxley Road, CV359JY ','yes'); -INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('North Hampton','1 Crispin Street, NN12JH','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('North Hampton','1 Crispin Street, NN12JH','No'); INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Barton Smith','87 Finchfiels Road, WV39LQ','Yes'); -INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Simone Malanga','56 Ashworth Road, WS115DS','Yes'); -INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Bobby Brown','51 Overdale Road, TF34BX','No'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Simone Malanga','56 Ashworth Road, WS115DS','Yes'); +INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Bobby Brown','51 Overdale Road, TF34BX','No'); INSERT INTO STUDENTS (name,address,CYF_graduate) VALUES ('Mathew Perry','10 Mathew Street, L34AA','Yes'); //verifying entries for students SELECT * FROM students; -//classes Table created +//classes Table created -CREATE TABLE classes(mentor_name VARCHAR(30), module VARCHAR(30),course_date DATE NOT NULL, course_location VARCHAR(30)); +CREATE TABLE classes(id SERIAL PRIMARY KEY, mentor_name VARCHAR(30), module VARCHAR(30),course_date DATE NOT NULL, course_location VARCHAR(30)); //Verify classes table created \d classes //Insert entries to classes table - + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Rody Kirwan','React', '2019-10-01','West Midland'); INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Jason Sancho','Javascript', '2019-11-05','West Midland'); - INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Andrew Jackson','Node', '2019-07-02','London'); + INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Andrew Jackson','Node', '2019-07-02','London'); INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Stella Markov','HTML', '2019-02-07','Manchester'); INSERT INTO classes(mentor_name , module ,course_date , course_location ) VALUES ('Sandeep Singh','CSS', '2019-03-09','Glascow'); @@ -84,7 +84,17 @@ CREATE TABLE classes(mentor_name VARCHAR(30), module VARCHAR(30),course_date DAT select * from classes; - +// specific classes taken by students by updating the students table +UPDATE students SET classes_id = 1 WHERE id = 1; +UPDATE students SET classes_id = 5 WHERE id = 2; +UPDATE students SET classes_id = 4 WHERE id = 3; +UPDATE students SET classes_id = 1 WHERE id = 4; +UPDATE students SET classes_id = 3 WHERE id = 5; +UPDATE students SET classes_id = 2 WHERE id = 6; +UPDATE students SET classes_id = 4 WHERE id = 7; +UPDATE students SET classes_id = 3 WHERE id = 8; +UPDATE students SET classes_id = 5 WHERE id = 9; +UPDATE students SET classes_id = 2 WHERE id = 10; ``` When you have finished all of the questions - open a pull request with your answers to the `Databases-Homework` repository. From 9df0d8ad21efb87a401ff9b11a5c841a673ee3af Mon Sep 17 00:00:00 2001 From: bireworld Date: Thu, 19 Nov 2020 08:39:48 +0000 Subject: [PATCH 4/9] all task completed --- week-1/mandatory/2-classes-db/task.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/week-1/mandatory/2-classes-db/task.md b/week-1/mandatory/2-classes-db/task.md index 9dc5e4ba..84e2f3ce 100644 --- a/week-1/mandatory/2-classes-db/task.md +++ b/week-1/mandatory/2-classes-db/task.md @@ -95,6 +95,26 @@ UPDATE students SET classes_id = 4 WHERE id = 7; UPDATE students SET classes_id = 3 WHERE id = 8; UPDATE students SET classes_id = 5 WHERE id = 9; UPDATE students SET classes_id = 2 WHERE id = 10; + +//Retrieve all the mentors who lived more than 5 years in Glasgow + +SELECT * FROM mentors WHERE years_in_glascow > 5; + +//Retrieve all the mentors whose favourite language is Javascript + +SELECT * FROM mentors WHERE fav_prog_language = 'Javascript'; + +//Retrieve all the students who are CYF graduates + +SELECT * FROM classes WHERE course_date < '2020-06-01'; + +//Retrieve all the students (retrieving student ids only is fine) who attended the Javascript class + +SELECT * FROM students WHERE classes_id = 2; +SELECT id FROM students WHERE classes_id = 2; + + + ``` When you have finished all of the questions - open a pull request with your answers to the `Databases-Homework` repository. From 69519f883c11b5e71fce4f1949e2b8764ccd72e5 Mon Sep 17 00:00:00 2001 From: bireworld Date: Sun, 22 Nov 2020 18:13:49 +0000 Subject: [PATCH 5/9] students and mentors database created --- week-2/mandatory/2-ecommerce-db/task.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index 70e9ce0c..f7bd04ef 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/task.md @@ -9,8 +9,11 @@ Below you will find a set of tasks for you to complete to set up a databases of To submit this homework write the correct commands for each question here: ```sql +//Database for students +createdb -p 5432 -U postgres students - +//Database for mentors +createdb -p 5432 -U postgres students ``` When you have finished all of the questions - open a pull request with your answers to the `Databases-Homework` repository. From bae1c023152d536489be314d883b349e299571f5 Mon Sep 17 00:00:00 2001 From: bireworld Date: Sun, 22 Nov 2020 18:25:48 +0000 Subject: [PATCH 6/9] cyf_ecommerce database ctreated --- week-2/mandatory/2-ecommerce-db/task.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index f7bd04ef..91815858 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/task.md @@ -23,7 +23,8 @@ When you have finished all of the questions - open a pull request with your answ To prepare your environment for this homework, open a terminal and create a new database called `cyf_ecommerce`: ```sql -createdb cyf_ecommerce +//createdb cyf_ecommerce +createdb -p 5432 -U postgres cyf_ecommerce ``` Import the file [`cyf_ecommerce.sql`](./cyf_ecommerce.sql) in your newly created database: From 03a728c54b9be6efd62e9de00fc5584682b822c2 Mon Sep 17 00:00:00 2001 From: bireworld Date: Sun, 22 Nov 2020 21:17:48 +0000 Subject: [PATCH 7/9] cyf_ecommerce solutions up to question 10 completed --- .../2-ecommerce-db/cyf_ecommerce.sql | 11 +++++++ week-2/mandatory/2-ecommerce-db/task.md | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql b/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql index 77253e2e..31a1ac88 100644 --- a/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql +++ b/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql @@ -103,3 +103,14 @@ INSERT INTO order_items (order_id, product_id, quantity) VALUES(8, 5, 1); INSERT INTO order_items (order_id, product_id, quantity) VALUES(9, 13, 2); INSERT INTO order_items (order_id, product_id, quantity) VALUES(10, 14, 1); INSERT INTO order_items (order_id, product_id, quantity) VALUES(10, 6, 5); + +SELECT customers.name, customers.address FROM customers WHERE country = 'United States'; +SELECT * FROM customers ORDER BY name ASC; +SELECT * FROM products WHERE unit_price > 100; +SELECT * FROM products WHERE product_name LIKE '%socks%'; +SELECT * FROM products ORDER BY unit_price DESC LIMIT 5; +SELECT product_name,unit_price,supplier_name FROM products INNER JOIN suppliers ON suppliers.id = products.id WHERE country = 'United Kingdom'; +SELECT product_name,supplier_name FROM suppliers INNER JOIN products ON suppliers.id = products.id WHERE country = 'United Kingdom'; +SELECT * FROM orders INNER JOIN customers on customers.id = orders.id; +SELECT * FROM orders INNER JOIN customers on customers.id = orders.id WHERE customers.name = 'Hope Crosby'; +SELECT product_name,unit_price,quantity FROM products INNER JOIN order_items ON order_items.id = products.id INNER JOIN orders ON orders.id = product_id WHERE order_reference = 'ORD006'; diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index 91815858..16940922 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/task.md @@ -40,14 +40,47 @@ Open the file `cyf_ecommerce.sql` in VSCode and make sure you understand all the 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 lives in United States + +`SELECT customers.name, customers.address FROM customers WHERE country = 'United States';` + 2. Retrieve all the customers ordered by ascending name + +`SELECT * FROM customers ORDER BY name ASC;` + 3. Retrieve all the products which cost more than 100 + +`SELECT * FROM products WHERE unit_price > 100;` + 4. Retrieve all the products whose name contains the word `socks` + +`SELECT * FROM products WHERE product_name LIKE '%socks%';` + 5. Retrieve the 5 most expensive products + +`SELECT * FROM products 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` + +`SELECT product_name,unit_price,supplier_name FROM products INNER JOIN suppliers ON suppliers.id = products.id WHERE country = 'United Kingdom';` + 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 product_name,supplier_name FROM suppliers INNER JOIN products ON suppliers.id = products.id WHERE country = 'United Kingdom';` + 8. Retrieve all orders from customer ID `1` + +`SELECT * FROM orders INNER JOIN customers on customers.id = orders.id;` + 9. Retrieve all orders from customer named `Hope Crosby` + +`SELECT * FROM orders INNER JOIN customers on customers.id = orders.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 product_name,unit_price,quantity FROM products INNER JOIN order_items ON order_items.id = products.id INNER JOIN orders ON orders.id = product_id WHERE 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`. + + + 12. Retrieve the names of all customers who bought a product from a supplier from China. From 571891124f0a693c539930f9202dbfeedfec62f1 Mon Sep 17 00:00:00 2001 From: bireworld Date: Sun, 29 Nov 2020 22:38:49 +0000 Subject: [PATCH 8/9] ecommerce task completed --- week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql | 2 ++ week-2/mandatory/2-ecommerce-db/task.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql b/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql index 31a1ac88..b99b6e05 100644 --- a/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql +++ b/week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql @@ -114,3 +114,5 @@ SELECT product_name,supplier_name FROM suppliers INNER JOIN products ON supplie SELECT * FROM orders INNER JOIN customers on customers.id = orders.id; SELECT * FROM orders INNER JOIN customers on customers.id = orders.id WHERE customers.name = 'Hope Crosby'; SELECT product_name,unit_price,quantity FROM products INNER JOIN order_items ON order_items.id = products.id INNER JOIN orders ON orders.id = product_id WHERE order_reference = 'ORD006'; +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 order_items.product_id = products.id INNER JOIN suppliers ON suppliers.id= products.supplier_id; +SELECT DISTINCT customers.name FROM customers, products, suppliers, orders ,order_items WHERE customers.id = orders.customer_id AND orders.id = order_items.order_id AND order_items.product_id = products.id AND suppliers.id = products.supplier_id AND suppliers.country='China'; diff --git a/week-2/mandatory/2-ecommerce-db/task.md b/week-2/mandatory/2-ecommerce-db/task.md index 16940922..4276349a 100644 --- a/week-2/mandatory/2-ecommerce-db/task.md +++ b/week-2/mandatory/2-ecommerce-db/task.md @@ -81,6 +81,6 @@ 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`. - +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 order_items.product_id = products.id INNER JOIN suppliers ON suppliers.id= products.supplier_id; 12. Retrieve the names of all customers who bought a product from a supplier from China. From d1e2e9353d810610ab00fe91724755f0183f109d Mon Sep 17 00:00:00 2001 From: bireworld Date: Sun, 29 Nov 2020 23:01:51 +0000 Subject: [PATCH 9/9] ecommerce Api task compeleted --- week-2/mandatory/3-api/package.json | 17 ++++++++++++ week-2/mandatory/3-api/server.js | 40 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 week-2/mandatory/3-api/package.json create mode 100644 week-2/mandatory/3-api/server.js diff --git a/week-2/mandatory/3-api/package.json b/week-2/mandatory/3-api/package.json new file mode 100644 index 00000000..e9b51fc4 --- /dev/null +++ b/week-2/mandatory/3-api/package.json @@ -0,0 +1,17 @@ +{ + "name": "cyf-ecommerce-api", + "version": "1.0.0", + "description": "E-Commerce API/week2", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "nodemon server.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.19.0", + "express": "^4.17.1", + "pg": "^8.5.1" + } +} \ No newline at end of file diff --git a/week-2/mandatory/3-api/server.js b/week-2/mandatory/3-api/server.js new file mode 100644 index 00000000..fc5194bf --- /dev/null +++ b/week-2/mandatory/3-api/server.js @@ -0,0 +1,40 @@ +const express = require("express") +const app = express() +const {Pool} = require("pg") + +app.use(express.json()); + +const pool = new Pool({ + user: "S225693", + host: "localhost", + database: "cyf_ecommerce", + password: "lucianome1", + port: 5432, +}); + +app.get("/customers", function (req, res) { + + pool.query("SELECT * FROM customers") + .then((result) => res.json(result.rows)) + .catch((e) => console.error(e)); +}); + + +app.get("/suppliers", function (req, res) { + pool + .query("SELECT * FROM suppliers") + .then((result) => res.json(result.rows)) + .catch((e) => console.error(e)); +}); +app.get("/products", function (req, res) { + pool + .query( + "SELECT products.product_name, suppliers.supplier_name FROM products INNER JOIN suppliers ON products.supplier_id = suppliers.id " + ) + .then((result) => res.json(result.rows)) + .catch((e) => console.error(e)); +}); + +app.listen(3001, function () { + console.log("Server is listening on port 3000. Ready to accept requests!"); +}); \ No newline at end of file