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
41 changes: 41 additions & 0 deletions week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@ Below you will find a set of tasks for you to complete to consolidate and extend
To submit this homework write the correct commands for each question here:

```sql
1. SELECT * FROM rooms WHERE rate>100;

2. SELECT *, (checkout_date-checkin_date) AS night FROM reservations
WHERE (extract(month from checkin_date)) = (extract(month from current_date))
AND (extract(year from checkin_date)) = (extract(year from current_date))
AND checkout_date-checkin_date>2;

3. SELECT * FROM customers WHERE city LIKE 'M%';

4. INSERT INTO room_types VALUES ('PENTHOUSE', 185.00);

5. INSERT INTO rooms (room_no, room_type, rate)
VALUES
(501, 'PENTHOUSE', (SELECT def_rate FROM room_types WHERE room_type='PENTHOUSE')),
(502, 'PENTHOUSE', (SELECT def_rate FROM room_types WHERE room_type='PENTHOUSE'));

6. INSERT INTO rooms (room_no, rate, room_type, no_guests)
VALUES (503, 143.00, 'PREMIER PLUS', (select no_guests from rooms where room_type='PREMIER PLUS' limit 1));

7. Display Which rooms, How many different checkin, How many nights were occupied during last month;

SELECT room_no, count(room_no) AS different_times, count(checkout_date-checkin_date) AS nights_total FROM reservations WHERE checkin_date>=date_trunc('month', now()-'1 month'::interval) AND checkout_date<now() GROUP BY room_no;

Display How many rooms and how many nights were occupied during last month;

SELECT count(room_no) AS room_total, sum(nights_total) AS nights_ttotal FROM
(SELECT room_no, count(room_no) as different_times, count(checkout_date-checkin_date) as nights_total from reservations
where checkin_date>=date_trunc('month', now()-'1 month'::interval) and checkout_date<now() group by room_no) as detailed_table;


8. SELECT sum (checkout_date-checkin_date) AS num_nights FROM reservations WHERE room_no BETWEEN 200 AND 300;

9. SELECT count(id) AS num_invoices, sum(total) AS total_invoices, avg(total) AS avg_invoices FROM invoices WHERE total>300;

10. SELECT * FROM
(SELECT sum(checkout_date-checkin_date) as FIRST_FLOOR FROM reservations WHERE room_no between 100 and 200) as F,
(SELECT sum(checkout_date-checkin_date) as SECOND_FLOOR FROM reservations WHERE room_no between 200 and 300) as S,
(SELECT sum(checkout_date-checkin_date) as THIRD_FLOOR FROM reservations WHERE room_no between 300 and 400) as T,
(SELECT sum(checkout_date-checkin_date) as FOURTH_FLOOR FROM reservations WHERE room_no between 300 and 400) as FO;


```
Expand All @@ -19,7 +58,9 @@ 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?

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

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

Insert some new data into the room_types and rooms tables, querying after each stage to check the data, as follows:
Expand Down
40 changes: 39 additions & 1 deletion week-2/mandatory/2-ecommerce-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Import the file [`cyf_ecommerce.sql`](./cyf_ecommerce.sql) in your newly created

```sql
psql -d cyf_ecommerce -f cyf_ecommerce.sql

```

Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a piece of paper and draw the database with the different relationships between tables (as defined by the REFERENCES keyword in the CREATE TABLE commands). Identify the foreign keys and make sure you understand the full database schema.
Expand All @@ -35,16 +36,53 @@ 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

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

2. Retrieve all the customers in ascending name sequence

select * from customers order by name desc;

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

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.

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

5. Retrieve the 5 most expensive products

select * from product_availability 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 p.product_name, pa.unit_price, s.supplier_name from products p join product_availability pa on p.id=pa.prod_id join suppliers s on pa.supp_id=s.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`.

select p.product_name, s.supplier_name from products p join product_availability pa on p.id=pa.prod_id join suppliers s on s.id=pa.supp_id where s.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).

select o.id, o.order_date, o.order_reference, sum(oi.quantity*pa.unit_price) as total_cost from orders o join order_items oi on o.id=oi.order_id join product_availability pa on oi.product_id=pa.prod_id join customers c on c.id=o.customer_id where c.id=1 group by o.id, o.order_date, o.order_reference;

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

select o.id, o.order_date, p.product_name from orders o join order_items oi on o.id=oi.order_id join products p on p.id=oi.product_id join customers c on c.id=o.customer_id where c.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 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 o.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 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_items oi on o.id=oi.order_id join products p on p.id=oi.product_id join suppliers s on s.id=oi.supplier_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.

select c.name from customers c join orders o on c.id=o.customer_id join order_items oi on o.id=oi.order_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.

select c.name, o.order_reference, o.order_date, sum(pa.unit_price*oi.quantity) as total_amount from customers c join orders o on c.id=o.customer_id join order_items oi on o.id=oi.order_id join product_availability pa on pa.prod_id=oi.product_id group by c.name, o.order_reference, o.order_date order by total_amount desc;
Loading