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

Denis p/week2 #55

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

//ANSWER:
createdb cyf_classes

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.

//ANSWER:

CREATE TABLE mentors(
id SERIAL PRIMARY KEY,
name VARCHAR(20),
years_in_glasgow VARCHAR(5),
address VARCHAR(30),
fav_programming_lang VARCHAR(20)
);

3. Insert 5 mentors in the `mentors` table (you can make up the data, it doesn't need to be accurate ;-)).

//ANSWER:

INSERT INTO mentors(id, name, years_in_glasgow, address, fav_programming_lang) VALUES ('1', 'Mentor1', '3', '10 High Street', 'JavaScript');
INSERT INTO mentors(id, name, years_in_glasgow, address, fav_programming_lang) VALUES ('2', 'Mentor2', '6', '20 High Street', 'C++');
INSERT INTO mentors(id, name, years_in_glasgow, address, fav_programming_lang) VALUES ('3', 'Mentor3', '2', '40 High Street', 'C#');
INSERT INTO mentors(id, name, years_in_glasgow, address, fav_programming_lang) VALUES ('4', 'Mentor4', '8', '82 High Street', 'Python');
INSERT INTO mentors(id, name, years_in_glasgow, address, fav_programming_lang) VALUES ('5', 'Mentor5', '1', '91 High Street', 'C');

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.

//ANSWER:

CREATE TABLE students(
id SERIAL PRIMARY KEY,
name VARCHAR(20),
address VARCHAR(30),
cyf_graduated VARCHAR(20)
);

5. Insert 10 students in the `students` table.

//ANSWER:

INSERT INTO students(id, name, address, cyf_graduated) VALUES ('1', 'Student1','10 Bake Street', 'yes');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('2', 'Student2','20 Bake Street', 'no');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('3', 'Student3','40 Bake Street', 'no');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('4', 'Student4','82 Bake Street', 'yes');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('5', 'Student5','91 Bake Street', 'no');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('6', 'Student6','10 Bake Street', 'yes');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('7', 'Student7','20 Bake Street', 'no');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('8', 'Student8','40 Bake Street', 'no');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('9', 'Student9','82 Bake Street', 'yes');
INSERT INTO students(id, name, address, cyf_graduated) VALUES ('10', 'Student10','91 Bake Street', 'yes');
6. Verify that the data you created for mentors and students are correctly stored in their respective tables (hint: use a `select` SQL statement).

//ANSWER:

SELECT * FROM mentors;
SELECT * FROM students;

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

//ANSWER:

CREATE TABLE classes(
id SERIAL PRIMARY KEY,
mentor VARCHAR(20),
topic VARCHAR(20),
date VARCHAR(15),
location VARCHAR(20)
);

8. Insert a few classes in the `classes` table

//ANSWER:

INSERT INTO classes(id, mentor, topic, date, location) VALUES ('1', 'Mentor1','JavaScript', '2020-01-02', 'Birmingham');
INSERT INTO classes(id, mentor, topic, date, location) VALUES ('2', 'Mentor2','HTML', '2020-01-09', 'London');
INSERT INTO classes(id, mentor, topic, date, location) VALUES ('3', 'Mentor3','CSS', '2020-01-16', 'Manchester');
INSERT INTO classes(id, mentor, topic, date, location) VALUES ('4', 'Mentor4','React', '2020-01-23', 'Glasgow');
INSERT INTO classes(id, mentor, topic, date, location) VALUES ('5', 'Mentor5','Node', '2020-02-01', 'London');


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.

//ANSWER:

CREATE TABLE classes_attended (
id SERIAL PRIMARY KEY,
class INT REFERENCES classes(id),
student INT REFERENCES students(id)
);


INSERT INTO classes_attended (id, class, student) VALUES (1, 1, 1);
INSERT INTO classes_attended (id, class, student) VALUES (2, 1, 2);
INSERT INTO classes_attended (id, class, student) VALUES (3, 2, 3);
INSERT INTO classes_attended (id, class, student) VALUES (4, 4, 1);
INSERT INTO classes_attended (id, class, student) VALUES (5, 3, 1);
INSERT INTO classes_attended (id, class, student) VALUES (6, 5, 4);

10. Answer the following questions using a `select` SQL statement:
- Retrieve all the mentors who lived more than 5 years in Glasgow
//ANSWER:
SELECT * FROM mentors WHERE years_in_glasgow > '5';
- Retrieve all the mentors whose favourite language is Javascript
//ANSWER:
SELECT * FROM mentors WHERE fav_programming_lang = 'JavaScript';
- Retrieve all the students who are CYF graduates
//ANSWER:
SELECT * FROM students WHERE cyf_graduated = 'yes';
- Retrieve all the classes taught before June this year
//ANSWER:
SELECT * FROM classes WHERE date < '2020-06-01';
- 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).
//ANSWER:
SELECT student FROM classes_attended WHERE class = '1';
12 changes: 12 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,18 @@ 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 product_name, unit_price, supplier_name FROM products INNER JOIN suppliers ON suppliers.id = products.supplier_id;
7. SELECT product_name, supplier_name FROM products INNER JOIN suppliers ON suppliers.country = 'United Kingdom';
8. SELECT * FROM orders WHERE customer_id = 1;
9. SELECT * FROM orders INNER JOIN customers ON customers.id = orders.customer_id WHERE customers.name = 'Hope Crosby';
10. SELECT product_name, unit_price, quantity FROM order_items INNER JOIN products ON products.id = order_items.product_id INNER JOIn orders ON orders.id = order_items.order_id WHERE orders.order_reference = 'ORD006';
11. 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 .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';


```
Expand Down
Loading