Skip to content
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
67 changes: 65 additions & 2 deletions week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Submission

Below you will find a set of tasks for you to complete to consolidate and extend your learning from this week. You will find it beneficial to complete the reading tasks before attempting some of these.
Below you will find a set of tasks for you to complete to consolidate and extend your learning from this week. You will find it beneficial to complete the reading tasks before attempting some of these.

To submit this homework write the correct commands for each question here:

Expand All @@ -18,19 +18,82 @@ When you have finished all of the questions - open a pull request with your answ
If you haven't completed all the exercises from this lesson then do that first.

### Tasks

1. Which rooms have a rate of more than 100.00?

```sql
select * from rooms where rate > 100.00;

```

2. List the reservations that have a checkin date this month and are for more than three nights.

```sql
select * from reservations where checkin_date between '2020-09-01' and '2020-09-30' and (checkout_date - checkin_date) > 3;

```

3. List all customers from cities that begin with the letter 'M'.

```sql
select * from customers where city like 'M%';

```

Insert some new data into the room_types and rooms tables, querying after each stage to check the data, as follows:

4. Make a new room type of PENTHOUSE with a default rate of £185.00

```sql
insert into room_types (room_type, def_rate) values ('PENTHOUSE', 150.00);/

```

5. Add new rooms, 501 and 502 as room type PENTHOUSE and set the room rate of each to the default value (as in the new room type).

```sql
insert into rooms(room_no, room_type, rate) values (501, 'PENTHOUSE', 150.00),
(502, 'PENTHOUSE', 150.00);

```

6. Add a new room 503 as a PREMIER PLUS type similar to the other PREMIER PLUS rooms in the hotel but with a room rate of 143.00 to reflect its improved views over the city.

```sql
insert into rooms(room_no, room_type, rate) values (503, 'PREMIER PLUS', 143.00);

```

Using what you can learn about aggregate functions in the w3schools SQL classes (or other providers), try:

7. The hotel manager wishes to know how many rooms were occupied any time during the previous month - find that information.

```sql
select count (distinct room_no) from reservations where checkin_date >= '2020-08-01' and checkout_date <= '2020-08-31';

```

8. Get the total number of nights that customers stayed in rooms on the second floor (rooms 201 - 299).

```sql
select sum (checkout_date - checkin_date) as total_nights from reservations
where room_no between 201 and 299;

```

9. How many invoices are for more than £300.00 and what is their grand total and average amount?
10. Bonus Question: list the number of nights stay for each floor of the hotel (floor no is the hundreds part of room number, e.g. room **3**12 is on floor **3**)

```sql
select count(total), sum(total),round(avg(total)) from invoices where total > 300.00;

```

10. Bonus Question: list the number of nights stay for each floor of the hotel (floor no is the hundreds part of room number, e.g. room **3**12 is on floor **3**)

```sql
select floor(room_no/100) as floor,
sum (checkout_date - checkin_date) as nights
from reservations
group by floor order by floor;

```
98 changes: 96 additions & 2 deletions week-2/mandatory/2-ecommerce-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -35,16 +36,109 @@ 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

```sql
select name, address from customers where country = 'United States';
```

2. Retrieve all the customers in ascending name sequence

```sql
select * from customers order by name;
```

3. Retrieve all the products whose name contains the word `socks`

```sql
select * from products where product_name like '%socks%';
```

4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id.

```sql
select pa.prod_id, p.product_name, pa.unit_price, pa.supp_id
from product_availability pa join products p on (pa.prod_id=p.id)
where unit_price > 100;
```

5. Retrieve the 5 most expensive products

```sql
select pa.prod_id, p.product_name, pa.unit_price, pa.supp_id
from product_availability pa join products p on (pa.prod_id=p.id)
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`

```sql
select p.product_name, pa.unit_price, s.supplier_name
from product_availability pa join products p on (pa.prod_id=p.id)
join suppliers s on (s.id = pa.supp_id);
```

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).

```sql
select p.product_name, s.supplier_name
from product_availability pa join suppliers s on (s.id = pa.supp_id)
join products p on (p.id = pa.prod_id)
where country = 'United Kingdom';
```

8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity \* unit price).

```sql
select o.id, o.order_reference, o.order_date, oi.quantity, oi.quantity*pa.unit_price as total_cost
from orders o join order_items oi on(o.id = oi.order_id)
join products p on(p.id = oi.product_id)
join product_availability pa on(pa.prod_id = p.id)
where customer_id = 1;
```

9. Retrieve all orders, including order items, from customer named `Hope Crosby`

```sql

```

10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`.

```sql
select p.product_name, pa.unit_price, oi.quantity
from orders o join order_items oi on(o.id = oi.order_id)
join products p on(p.id = oi.product_id)
join product_availability pa on(pa.prod_id = p.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`.

```sql
select c.name, o.order_reference, o.order_date, p.product_name, s.supplier_name, oi.quantity
from customers c join orders o on(c.id = o.customer_id)
join order_item oi on(oi.order_id = o.id)
join suppliers s on(s.id = oi.supplier_id)
join product_availability pa on (pa.supp_id = s.id)
join products p on(p.id = pa.prod_id);
```

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.

```sql
select distinct name from customers c join orders o on(c.id = o.customer_id)
join order_items oi on(oi.order_id = o.id)
join suppliers s on(s.id = oi.supplier_id)
where s.country = 'China';
```

13. List all orders giving customer name, order reference, order date and order total amount (quantity \* unit price) in descending order of total.

```sql
select c.name, o.order_reference, o.order_date, oi.quantity*pa.unit_price as total_cost
from orders o join customers c on(o.customer_id = c.id)
join order_items oi on (o.id = oi.order_id)
join products p on (p.id = oi.product_id)
join product_availability pa on(pa.prod_id = p.id)
order by total_cost desc;
```
124 changes: 124 additions & 0 deletions week-2/mandatory/3-api/cyf_ecommerce.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
drop table if exists order_items;
drop table if exists orders cascade;
DROP TABLE IF EXISTS product_availability cascade;
drop table if exists customers cascade;
drop table if exists products cascade;
drop table if exists suppliers cascade;

CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
address VARCHAR(120),
city VARCHAR(30),
country VARCHAR(20)
);

CREATE TABLE suppliers (
id SERIAL PRIMARY KEY,
supplier_name VARCHAR(100) NOT NULL,
country VARCHAR(20) NOT NULL
);

CREATE TABLE products (
id SERIAL PRIMARY KEY,
product_name VARCHAR(100) NOT NULL
);

create table product_availability (
prod_id integer references products(id),
supp_id integer references suppliers(id),
unit_price integer not null,
primary key (prod_id, supp_id)
);

CREATE TABLE orders (
id SERIAL PRIMARY KEY,
order_date DATE NOT NULL,
order_reference VARCHAR(10) NOT NULL,
customer_id INT REFERENCES customers(id)
);

CREATE TABLE order_items (
id SERIAL PRIMARY KEY,
order_id INT NOT NULL REFERENCES orders(id),
product_id INT NOT NULL,
supplier_id INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (product_id, supplier_id)
REFERENCES product_availability (prod_id, supp_id)
);

INSERT INTO customers (name, address, city, country) VALUES ('Guy Crawford','770-2839 Ligula Road','Paris','France');
INSERT INTO customers (name, address, city, country) VALUES ('Hope Crosby','P.O. Box 276, 4976 Sit Rd.','Steyr','United Kingdom');
INSERT INTO customers (name, address, city, country) VALUES ('Britanney Kirkland','P.O. Box 577, 5601 Sem, St.','Little Rock','United Kingdom');
INSERT INTO customers (name, address, city, country) VALUES ('Amber Tran','6967 Ac Road','Villafranca Asti','United States');
INSERT INTO customers (name, address, city, country) VALUES ('Edan Higgins','Ap #840-3255 Tincidunt St.','Arles','United States');
INSERT INTO customers (name, address, city, country) VALUES ('Quintessa Austin','597-2737 Nunc Rd.','Saint-Marc','United Kingdom');

INSERT INTO suppliers (supplier_name, country) VALUES ('Amazon', 'United States');
INSERT INTO suppliers (supplier_name, country) VALUES ('Taobao', 'China');
INSERT INTO suppliers (supplier_name, country) VALUES ('Argos', 'United Kingdom');
INSERT INTO suppliers (supplier_name, country) VALUES ('Sainsburys', 'United Kingdom');


INSERT INTO products (product_name) VALUES ('Mobile Phone X');
INSERT INTO products (product_name) VALUES ('Javascript Book');
INSERT INTO products (product_name) VALUES ('Le Petit Prince');
INSERT INTO products (product_name) VALUES ('Super warm socks');
INSERT INTO products (product_name) VALUES ('Coffee Cup');
INSERT INTO products (product_name) VALUES ('Ball');
INSERT INTO products (product_name) VALUES ('Tee Shirt Olympic Games');

INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (1, 4, 249);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (1, 1, 299);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 2, 41);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 3, 39);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (2, 1, 40);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (3, 4, 10);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (3, 1, 10);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 4, 10);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 3, 8);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 2, 5);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (4, 1, 10);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 4, 5);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 3, 4);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 2, 4);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (5, 1, 3);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 2, 20);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 4, 15);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (6, 1, 14);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 3, 21);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 2, 18);
INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES (7, 1, 20);

INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-06-01', 'ORD001', 1);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-15', 'ORD002', 1);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-11', 'ORD003', 1);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-24', 'ORD004', 2);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-30', 'ORD005', 3);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-05', 'ORD006', 4);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-04-05', 'ORD007', 4);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-23', 'ORD008', 5);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-24', 'ORD009', 5);
INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-10', 'ORD010', 5);

INSERT INTO order_items VALUES (1, 1, 7, 2, 1);
INSERT INTO order_items VALUES (2, 1, 4, 2, 5);
INSERT INTO order_items VALUES (3, 2, 4, 3, 4);
INSERT INTO order_items VALUES (4, 2, 3, 4, 1);
INSERT INTO order_items VALUES (5, 3, 5, 3, 10);
INSERT INTO order_items VALUES (6, 3, 6, 2, 2);
INSERT INTO order_items VALUES (7, 4, 1, 1, 1);
INSERT INTO order_items VALUES (8, 5, 2, 3, 2);
INSERT INTO order_items VALUES (9, 5, 3, 1, 1);
INSERT INTO order_items VALUES (10, 6, 5, 2, 3);
INSERT INTO order_items VALUES (11, 6, 2, 2, 1);
INSERT INTO order_items VALUES (12, 6, 3, 4, 1);
INSERT INTO order_items VALUES (13, 6, 4, 4, 3);
INSERT INTO order_items VALUES (14, 7, 4, 3, 15);
INSERT INTO order_items VALUES (15, 8, 7, 1, 1);
INSERT INTO order_items VALUES (16, 8, 1, 4, 1);
INSERT INTO order_items VALUES (17, 9, 6, 4, 2);
INSERT INTO order_items VALUES (18, 10, 6, 2, 1);
INSERT INTO order_items VALUES (19, 10, 4, 1, 5);

1 change: 1 addition & 0 deletions week-2/mandatory/3-api/cyf_ecommerce/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Loading