Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql, case when & top 1 14 26
sql server query 18 33
SQL Availablity Groups List 2 6
SQL Recursion 6 16
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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

790 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