Solved

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

Posted on 2012-03-13
2
370 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

763 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