Joins

A join links the rows in two or more tables by comparing the values in key columns and returning rows that have matching values. For example, this query joins the SalesOrderItems, Products, Employees, and SalesOrders tables to calculate sales totals and revenue:

SELECT Products.ID AS 'Product Code', 
  Products.Name AS Item, 
  Products.Description AS Style, 
  SUM(SalesOrderItems.Quantity) AS Sales,  
    Products.UnitPrice, 
  SUM(SalesOrderItems.Quantity * Products.UnitPrice) 
    AS Revenue FROM Employees
  JOIN SalesOrders ON SalesOrders.SalesRepresentative =
    Employees.EmployeeID 
  JOIN SalesOrderItems ON SalesOrderItems.ID =
    SalesOrders.ID
  JOIN Products ON Products.ID  = 
    SalesOrderItems.ProductID 
GROUP BY Products.ID, Products.Description,
  Products.Name, Products.UnitPrice
ORDER BY Sales