-
Notifications
You must be signed in to change notification settings - Fork 44
DB week_1 homework #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
zoltan-gal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work Razan! I'm really impressed by what a nice solution you found for the bonus question!
| 7- select count(room_no) | ||
| from reservations where checkin_date between '2020-08-01'and '2020-08-31' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More precisely count(distinct room_no), otherwise, you will get how many times rooms were occupied. distinct keyword removes duplicates before counting so that we will get how many different rooms were occupied.
| from reservations where checkin_date between '2020-08-01'and '2020-08-31' | ||
| 8- select sum(checkout_date - checkin_date) from reservations where room_no between 201 and 299; | ||
| 9- select count(*), sum(total), avg(total) from invoices where total >300; | ||
| 10- select sum(checkout_date - checkin_date) from reservations group by room_no/100 ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very elegant solution! One more advice, it could be made more information by adding a floor number to the query.
E.g.: select trunc(room_no/100) AS floor, um(checkout_date - checkin_date) from reservations group by room_no/100;
zoltan-gal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work Razan! I'd recommend doing some extra checks when working with joins to make sure you will get the rows joined in the expected combination. Try to check which rows in what combination you would like to see before actually joining them by selecting those first without a join. You can also make a scratch on paper what is your expectation. Finally, always ascertaina how many rows your join should returns, you can select a smaller subset initially as an example that is managable mentally.
I'm also looking forward to seeing the 3rd exercise from this HW pack.
| select a.prod_id, p.product_name, a.unit_price, a.supp_id from products p join product_availability a on (a.prod_id = p.id) where a.unit_price >100 ; | ||
|
|
||
| 5. Retrieve the 5 most expensive products | ||
| select * from product_availability order by unit_price DESC limit 5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine but would be nice to see product info as well.
| select o.order_reference, o.order_date from orders o join order_items i on (i.order_id = o.id) join product_availability a on (a.prod_id = i.product_id) where o.customer_id = 1; | ||
| ////////////I don't understand what they exactly want from this question //////////////// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's break it down
- Retrieve all orders, including order items for the customer 1
- Into the result, include:
- order id,
- order reference,
- order date
- total cost of the order
that is calculated as sum of (product quantity * product unit price).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're on the right track. Just need to add some extra column such as order id and the figure to calculate to end price by grouping the same order.
| 9. Retrieve all orders, including order items, from customer named `Hope Crosby` | ||
| select o.* from orders o join customers c on (o. customer_id = c.id) where c.name = 'Hope Crosby'; | ||
| ////// what do yo mean by including order items //////////////////////////// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume order items refers to the order_items table.
| select p.product_name , a.unit_price, i.quantity from orders o join order_items i on(i.order_id = o.id) join product_availability a on (i.supplier_id = a.supp_id) join products p on (p.id = a. prod_id) | ||
| where o.order_reference = 'ORD006'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be more intuitive joining the product availability through the product table and also add a join condition for connecting product on product availability table (product_availability a on (a.prod_id = p.id and a.supp_id = i.supplier_id)) to ensure you get the right combination, otherwise, join product_availability a on (i.supplier_id = a.supp_id) will join non-corresponding product rows that are still from the same suppliers.
e.g.
select p.product_name, a.unit_price, i.quantity
from orders o join
order_items i on (i.order_id = o.id) join
products p on (i.product_id = p.id) join
product_availability a on (a.prod_id = p.id and a.supp_id = i.supplier_id)
where o.order_reference = 'ORD006';
| select c.name from customers c join orders o on(c.id= o.customer_id) join | ||
| order_items i on (o.id = i. order_id) join | ||
| suppliers s on (s.id =i.supplier_id) | ||
| where s.country = 'China'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's okay but as an improvement, it is worth adding a distinct to remove duplicates if we are not interested in how many orders a customer has had.
select distinct c.name
from customers c join
orders o on (o.customer_id = c.id) join
order_items i on (i.order_id = o.id) join
suppliers s on (i.supplier_id = s.id)
where s.country = 'China';
| select c.name, o.order_reference, o.order_date, sum (quantity * unit_price) as total | ||
| from customers c join orders o on(c.id= o.customer_id) join | ||
| order_items i on (o.id = i. order_id) join | ||
| product_availability a on (i.supplier_id = a.supp_id) | ||
| group by c.name, o.order_reference, o.order_date | ||
| ORDER BY totaL DESC ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, you did it here what hadn't understood here:
- 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.order_reference, o.order_date from orders o join order_items i on (i.order_id = o.id) join product_availability a on (a.prod_id = i.product_id) where o.customer_id = 1;
////////////I don't understand what they exactly want from this question ////////////////
The only problem with the query is the same as the aforementioned.
You should and also add a join condition for connecting products on product availability table (product_availability a on (a.prod_id = p.id and a.supp_id = i.supplier_id)) to ensure you get the right combination from that table, otherwise, it will join more rows than necessary and the result will be incorrect.
select c.name, o.order_reference, o.order_date, sum(i.quantity * a.unit_price) as total
from customers c join
orders o on (o.customer_id = c.id) join
order_items i on (i.order_id = o.id) join
product_availability a on (i.supplier_id = a.supp_id and i.product_id = a.prod_id)
group by c.name, o.order_reference, o.order_date
order by total desc;
No description provided.