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

Sql week2 nihal #58

Open
wants to merge 3 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
94 changes: 85 additions & 9 deletions week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,81 @@ To submit this homework write the correct commands for each question here:

```sql

CREATE DATABASE cyf_classes;

//Create the mentors table:

CREATE TABLE mentors (
id SERIAL PRIMARY KEY, name varchar(200) NOT NULL, years_in_Glasgow INT(100) NOT NULL, address varchar(300) NOT NULL, favourate_programming_language varchar(100) NOT NULL
);

//Insert the mentors data:

INSERT INTO mentors values(1,'ola', 10, 'London', 'JavaScript'),
(2, 'Donald', 4, 'glasgow', 'C'),
(3,'Martin', 11, 'Birmingham', 'Java'),
(4,'Sanna', 6, 'London', 'C'),
(5,'Hind', 1, 'Bristol', 'C');

//Create the student table:

CREATE TABLE student (
name varchar (100) NOT NULL, address varchar(300) NOT NULL, graduated BOOLEAN NOT NULL

);

// Insert student data:

INT INTO student values ('Noor', 'Ladywood' , False),
('Lama', 'Birmingham' , False),
('Nihal', 'Manchester' , True),
('Omar', 'London' , True),
('Sozan', 'London' , False),
('Dorman', 'Darby' , False),
('Soliman', 'Lister' , True),
('Sammar', 'Lister' , False),
('Hossain', 'London' , True),
('Ali', 'Birmingham' , True),
('Rana', 'Birmingham' , False);

// Select from the student table:

select\*from students;


// Create classes table:

CREATE TABLE classes (
id SERIAL PRIMARY KEY, leading_mentor varchar(100), topic varchar(300) primary key, class_date date, class_location varchar(300)

);

//Insert the data:

INSERT INTO classes values(1,'Noorhan', 'C++', '01-02-2021', 'center building'),
(2, 'Ali', 'Node JS', '01-02-2021', 'main tower'),
(3, 'Susan', 'JavaScrept', '28-08-2020', ' main tower'),
(4, 'Ala', 'C++', '01-12-2020', 'CLT building'),
(5, 'Flora', 'Java', '01-03-2020', 'center building'),
(6, 'Mosa', 'C++', '20-05-2020', ' center building');

g
//9-Create table to show students attends:

CREATE TABLE student_attends (student_id INT REFERENCES student(id),class_id INT REFERENCES classes(id));
INSERT INTO student_attends(student_id,class_id)values
(0,1),
(8,1),
(3,3)
(1,0);

//select from mentor:
select * from mentor where years_in_Glasgow > 5;
select * from mentor where favourate_programming_language = 'JavasCript';
select * from student where graduated = True;
select * from classes where class_date < '2020-06-01';
select * from student_attends where topic = 'Node JS';


```

Expand All @@ -16,22 +91,23 @@ When you have finished all of the questions - open a pull request with your answ
## Task

1. Create a new database called `cyf_classes` (hint: use `createdb` in the terminal)
2. Create a new table `mentors`, for each mentor we want to save their name, how many years they lived in Glasgow, their address and their favourite programming language.
2. Create a new table `mentors`, for each mentor we want to save their name, how many years they lived in mentors, their address and their favourite programming language.
3. Insert 5 mentors in the `mentors` table (you can make up the data, it doesn't need to be accurate ;-)).
4. Create a new table `students`, for each student we want to save their name, address and if they have graduated from Code Your Future.
5. Insert 10 students in the `students` table.
6. Verify that the data you created for mentors and students are correctly stored in their respective tables (hint: use a `select` SQL statement).
7. Create a new `classes` table to record the following information:

- A class has a leading mentor
- A class has a topic (such as Javascript, NodeJS)
- A class is taught at a specific date and at a specific location
- A class has a leading mentor
- A class has a topic (such as Javascript, NodeJS)
- A class is taught at a specific date and at a specific location

8. Insert a few classes in the `classes` table
9. We now want to store who among the students attends a specific class. How would you store that? Come up with a solution and insert some data if you model this as a new table.
10. Answer the following questions using a `select` SQL statement:
- Retrieve all the mentors who lived more than 5 years in Glasgow
- Retrieve all the mentors whose favourite language is Javascript
- Retrieve all the students who are CYF graduates
- Retrieve all the classes taught before June this year
- Retrieve all the students (retrieving student ids only is fine) who attended the Javascript class (or any other class that you have in the `classes` table).
- Retrieve all the mentors who lived more than 5 years in Glasgow
- Retrieve all the mentors whose favourite language is Javascript
- Retrieve all the students who are CYF graduates
- Retrieve all the classes taught before June this year
- Retrieve all the students (retrieving student ids only is fine) who attended the Javascript class (or any other class that you have in the `classes` table).

27 changes: 27 additions & 0 deletions week-2/mandatory/2-ecommerce-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ 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
1-SELECT name, address FROM customers WHERE country = 'United States';
2-SELECT * FROM customers ORDER BY name;
3- SELECT * FROM products WHERE unit_price > 100;
4- SELECT * FROM products WHERE product_name LIKE '%socks%';
5- SELECT * FROM products ORDER BY unit_price DESC LIMIT 5;
6-SELECT products.product_name, products.unit_price, suppliers.supplier_name FROM products
INNER JOIN suppliers ON suppliers.id = products.supplier_id;
7-SELECT products.product_name,suppliers.supplier_name From products
INNER JOIN suppliers ON suppliers.id = products.supplier_id
WHERE country = 'United Kingdom';
8-SELECT * FROM orders WHERE customer_id = '1';
9-SELECT customers.name,customers.id, orders.order_reference FROM orders
INNER JOIN customers ON customers.id = orders.customer_id
where customers.name = 'Hope Crosby';
10-SELECT products.product_name, products.unit_price, order_items.quantity,
11- SELECT customers.name, orders.order_reference, orders.order_date , products.product_name, suppliers.supplier_name, order_items.quantity from orders
INNER JOIN order_items ON order_items.id = orders.id
INNER JOIN products ON products.id = order_items.product_id
INNER JOIN suppliers ON suppliers.id = products.supplier_id
INNER JOIN customers ON customers.id = orders.customer_id;

12- SELECT customers.name, suppliers.country FROM orders
INNER JOIN order_items ON order_items.id = orders.id
INNER JOIN products ON products.id = order_items.product_id
INNER JOIN suppliers ON suppliers.id = products.supplier_id
INNER JOIN customers ON customers.id = orders.customer_id
WHERE suppliers.country = 'China';


```
Expand Down
19 changes: 19 additions & 0 deletions week-2/mandatory/3-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "cyf-ecomerce-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pg": "^8.5.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}

34 changes: 34 additions & 0 deletions week-2/mandatory/3-api/sever.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const express = require("express");
const app = express();
const { Pool } = require('pg');
require('dotenv').config()

const pool = new Pool({
user: process.env.USERNAME,
host: 'localhost',
database: 'cyf_ecommerce',
password: process.env.PASSWORD,
port: 5432
});

app.get("/customers", function(req, res) {
pool.query('SELECT * FROM customers', (error, result) => {
res.json(result.rows);
});
});

app.get("/suppliers", function(req, res) {
pool.query('SELECT * FROM suppliers', (error, result) => {
res.json(result.rows);
});
});

app.get("/products", function(req, res) {
pool.query('SELECT products.product_name, suppliers.supplier_name FROM products INNER JOIN suppliers ON suppliers.id = products.supplier_id', (error, result) => {
res.json(result.rows);
});
});

app.listen(3000, function() {
console.log("Server is listening on port 3000. Ready to accept requests!");
});