MULTIPLE-TABLE SELECT

 

Examples

 

   Run the query - notice that there is one row per product.   Then switch to SQL view:


SELECT Categories.CategoryID, Categories.CategoryName, Products.ProductName, Sum([Order Details Extended].ExtendedPrice) AS ProductSales

FROM Categories
     INNER JOIN (Products
          INNER JOIN (Orders
               INNER JOIN [Order Details Extended]
               ON Orders.OrderID=[Order Details Extended].OrderID)
          ON Products.ProductID=[Order Details Extended].ProductID)
     ON Categories.CategoryID=Products.CategoryID

WHERE (((Orders.OrderDate) Between #1/1/1997# And #12/31/1997#))

GROUP BY Categories.CategoryID, Categories.CategoryName, Products.ProductName

ORDER BY Categories.CategoryName;


Notes:
The number of joins is equal to the total number of tables (or views) minus one.
A join condition (ON table1.col1 = table2.col2) must be specified for each join.

If the join is in the WHERE clause, the rules are the same - the minimum number of join criteria is equal to the number of tables (or views) minus one.

The GROUP BY clause summarizes data in subsets, in this case giving one row per product.  (Topic to be covered in detail in the third class)

The order of clauses in the SQL statement is important:  GROUP BY after WHERE (if present), ORDER BY last.
            

             

 Note:  Relationships among the six tables are not linear so it is harder to "see" them in the SQL statement.

 

Exercises

 


Tip:  To analyze or troubleshoot a query in the Access query window or in the command line utility in Oracle or MySQL, try breaking the statement as shown in the syntax diagram, with the keywords at the beginning of the lines; or copy and paste to a text editor (e.g., Notepad) and rearrange there.