Learn From - Interactive SQL


SQL JOIN 3 Tables


Here we use 3 Tables Customers, Product and OrderDetails.

Below is shown the "Customers" table:

CustomerID CustomerName EmailID Address City PostalCode Country
C001 Fredrick Johnson FJohnson@yahoo.com Berkeley Gardens 12 Brewery London WX1 6LT UK
C002 Mary Davidson Mary@gmail.com 35 King George Madrid 28023 Spain
C003 George Clooney George@hotmail.com Obere Str. 57 Berlin 12209 Germany
C004 David Louis David@mail.com 2743 Bering St. NewJersy 07019 USA
C005 Johnny Anderson Johnny@gmail.com 55 Grizzly Peak Rd. NewJersy 07019 USA
C006 Paul Henriot PaulHenriot@yahoo.com 59 rue de l'Abbaye Paris 51100 France
C007 Henry King Henry@mail.com 120 Hanover Sq. London WA1 1DP UK
C008 Robert John Robert@gmail.com 87 Polk St. Suite 5 San Francisco 94117 USA

Below is shown the "Product" table:

ProductID ProductDesc StockQuantity ProductPrice
Prd001 DVD Player 23 85.50
Prd002 Mobile Phone 45 70.33
Prd003 Laptop 15 250.45
Prd004 Watch 42 33.45
Prd005 Camera 34 95.75

Show Below is the "OrderDetails" table:

OrderID CustomerID ProductID QuantitySold
D001 C001 Prd001 2
D001 C001 Prd002 1
D001 C001 Prd005 1
D002 C004 Prd002 1
D002 C004 Prd004 3
D003 C006 Prd001 1
D003 C006 Prd002 1
D003 C006 Prd003 1
D003 C006 Prd004 2

SQL Using INNER JOIN


In the "OrderDetails" table OrderID is the Primary Key. CustomerID and ProductID are Foreign Keys.
In the table named "Customers", Column CustomerID is the Primary Key.
In the table named "Product", Column ProductID is the Primary Key.

To display the OrderDetails table details along with Customer Name and Product Description. Need to use the CustomerID and ProductID columns that are Foreign Keys in "OrderDetails" table.

Text Editor below has the SQL Query, click the "View Result" button, to see the result.

Select OrderDetails.OrderID, OrderDetails.CustomerID, Customers.CustomerName,
      OrderDetails.ProductID, Product.ProductDesc, OrderDetails.QuantitySold
From OrderDetails
INNER JOIN Customers on OrderDetails.CustomerID = Customers.CustomerID
INNER JOIN Product on OrderDetails.ProductID = Product.ProductID

SQL in Text Editor: