Q:

SELECT with condition on second table, show information from 3 tables

I m try to make a select from one table based on register of a second table
i m using 4 tables
table Products
Product_Id     |  Product_Name    |   Product_Value   | Category_Id
1              |  Ball            |   10              | 1
2              |  Pen             |    2              | 2 
3              |  Eraser          |    1              | 1

table Categories
Category_Id    |  Category_Name
1              |  Acessories
2              |  Others

table Customers
Customer_Id    |  Customer_Name
1              |  John
2              |  Peter

table Sells
Sell_Id        |  Product_Id      |   Customer_Id   |  Sell_Date
1              |  1               |   1             |  2012-01-01
2              |  2               |   1             |  2012-01-02

So i want to make a select to show on customer historic what he buy
like
John Historic
Product Name  Category Name   Date
Ball          Accessories     2012-01-01
Pen           Others          2012-010

On final i am showing information form 3 different tables
I have try using INNER JOIN and LEFT JOIN but with no success.
Thanks for any help.

A:

Break it down: THe first thing you want is a Product Name:
SELECT Product_Name FROM Products

How do we get the category? Join it in!
SELECT Products.Product_Name, Categories.Category_Name 
FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id

Getting close! Time to bring in the order history:
SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date 
FROM Products 
JOIN Categories ON Products.Category_Id = Categories.Category_Id
JOIN Sells ON Sells.Sells_Id = Products.Product_Id

Now we can bring in the name, and filter to John:
SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date 
FROM Products 
JOIN Categories ON Products.Category_Id = Categories.Category_Id
JOIN Sells ON Sells.Sells_Id = Products.Product_Id
JOIN Customers ON Sells.Customer_Id = Customers.Customer_Id
WHERE Customers.Customer_Name = 'John'

Just take it one step at a time in your mind. Build the query from each part as you go.
Since you mentioned it...
LEFT JOIN is how you deal with missing data. It returns rows from the "left" table, and puts NULL in the fields of the "right" table if there is no match for the ON clause. 
For example, if John bought a Product with no category (Product.Category_Id was NULL), using a JOIN would not return that record of purchase because there was no matching Category with Category_Id Null. In that case, you could LEFT JOIN Categories and in the result set, Category_Name would be NULL as well.

