Skip to content

Conversation

@roza8765
Copy link

No description provided.

Copy link

@zoltan-gal zoltan-gal left a 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!

Comment on lines +16 to +17
7- select count(room_no)
from reservations where checkin_date between '2020-08-01'and '2020-08-31'

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 ;

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;

Copy link

@zoltan-gal zoltan-gal left a 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;

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.

Comment on lines +59 to +60
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 ////////////////

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

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.

Comment on lines 62 to +64
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 ////////////////////////////

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.

Comment on lines +67 to +68
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';

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';

Comment on lines +79 to +82
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';

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';

Comment on lines +86 to +91
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 ;

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:

  1. 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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants