Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What happens when two "INNER JOIN" statements are used in one SQL query?

Posted on 2012-03-13
2
Medium Priority
?
374 Views
Last Modified: 2012-03-18
SELECT Products.ProductID, Products.ProductName, Products.SupplierID, Products.CategoryID, Products.QuantityPerUnit, Products.UnitPrice, Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel, Categories.CategoryName, Suppliers.CompanyName FROM Products INNER JOINCategories ON Products.CategoryID = Categories.CategoryID INNER JOIN Suppliers ONProducts.SupplierID = Suppliers.SupplierID

Open in new window


Hi, I'm trying to understanding what is happening in the Northwind database query above. I understand the "SELECT" statement but everything from the "FROM" statement onwards is a bit confusing to me. I was hoping that someone could break it down for me.

Also, shouldn't the Suppliers.SupplierID and Categories.CategoeryID be in the SELECT statement? And Products has a "many to one" relationship with Suppliers and Categories, but what would happen with this statement if the relationship was "one to many"? Would I end up with duplicate Products?
0
Comment
Question by:Eindoofus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 2000 total points
ID: 37717501
With an INNER JOIN what you're saying is that you only want to retrieve the rows from table A and B that have some matching criteria.

So in the above example the first INNER JOIN is only pulling records where the CategoryIDs match between the Products and Categories tables. The second INNER JOIN is filtering the results further by including only the results where the SupplierIDs match between the Products an Suppliers tables.

No - you don't have to include the joining columns in the select statement when doing a join.

If the relationship were switched to a "one to many" (i.e. one product can have one or more categories and one or more suppliers) then the database structure would be a little different. There are a few ways to implement this, but one way would be to add a ProductID column on the Categories and Suppliers tables. Your statement would then look something like this...

SELECT *
FROM Products
INNER JOIN Categories
ON Categories.ProductID = Products.ProductID
INNER JOIN Suppliers
ON Suppliers.ProductID = Product.ProductID

You won't be able to have duplicate products because of the ProductID primary key constraint; however, there may appear to be duplicates in your result set because you are joining data from multiple tables.
0
 

Author Closing Comment

by:Eindoofus
ID: 37718034
Great answer! Thank you :)
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question