Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Berhane/week 2 #56

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
51 changes: 51 additions & 0 deletions week-1/mandatory/2-classes-db/cyf_classes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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));



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');

106 changes: 106 additions & 0 deletions week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,112 @@ 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));

// 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 (id SERIAL PRIMARY KEY, 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(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 ('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;

// 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;

//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;



```

Expand Down
13 changes: 13 additions & 0 deletions week-2/mandatory/2-ecommerce-db/cyf_ecommerce.sql
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ 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';
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';
41 changes: 39 additions & 2 deletions week-2/mandatory/2-ecommerce-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,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:
Expand All @@ -36,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`.

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.
17 changes: 17 additions & 0 deletions week-2/mandatory/3-api/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
40 changes: 40 additions & 0 deletions week-2/mandatory/3-api/server.js
Original file line number Diff line number Diff line change
@@ -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!");
});