I am trying to write an SQL Query to extract the brands purchased by the customers? I have tried joining the tables but I am not able to execute it

Viewed 42

Here I have tried to inner join the table but I am unable to execute it properly

SELECT brand_name.Production,
FROM Production
INNER JOIN Sales ON Production.brand_id=Sales.brand_id; 

I am fairly new to SQL, please guide me through.

enter image description here

1 Answers

You need to make subqueries to select,in order:the orders the customer made from the orders table using order_id,then you need to select the products he purchased from the products table using product_id,then the brand of the product from the brands table using brand_id and from the brands table you can select the name of the brands. The queries should look something like this:

SELECT brands.brand_name
FROM brands
WHERE brands.brand_id IN (SELECT products.brand_id 
                                    FROM products 
                                    WHERE product_id IN(SELECT order_items.product_id
                                                                FROM order_items
                                                                WHERE order_id IN (SELECT orders.order_id FROM orders WHERE customer_id IN ('Insert query to find whatever id you want')
Related