Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now