Joeyrodrigues92
2/14/2017 - 12:38 AM

sql question

sql question

given these four tables, write a sql query to

1. return all of the products (in one query)

select *
from products;

2. return all of the products and the department they belong to (in one query)

select * 
from products
left join departments
on products.department_id = departments.id;

3. return all of the quantity purchased, the product name that was bought, the user's name who bought it, the department that the product belongs to (in one query)

select s.quantity, p.name as product_name, u.name as username, d.name as department_name
from sales s
left join products p
on p.id = s.product_id
left join users u
on u.id = s.user_id
left join departments d
on d.id = p.department_id


users: id, name, address, credit_card, credit_card_type

departments: id, name

products: id, name, price, department_id

sales: id, product_id, user_id, quantity