Solved

SQL Inner Join Question

Posted on 2012-03-27
6
357 Views
Last Modified: 2012-03-28
I have an SQL Statement that joins Table A - "Products" with Table B - "Brands" based on a BrandID.  

Brand information isn't required.  so in many cases, there is no BrandID in the Products Table.  The value for BrandID in the Products Table is 0 when no Brand is assigned.

For those products that do have a BrandID, I need to grab the related BrandName .  For those that don't have a BrandID, I still need them included in the results.

My statment looks like this:

"Select products.id, products.desc, products.details, brands.BrandName, products.IDBrand  from products inner join Brands on brands.BrandID = products.BrandID"

I'm hoping to return all the products in the database, but this will only return those that can be joined with the Brand table.  I'm not sure how to work around this.

Any ideas?

Thanks for your help!!!
0
Comment
Question by:slightlyoff
[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
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37773423
Select products.id, products.desc, products.details, brands.BrandName, products.IDBrand  from products left join Brands on products.BrandID = brands.BrandID
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 37773443
You need to do an OUTER JOIN like LEFT OUTER JOIN instead of INNER JOIN for this to work. Inner joins only get the information where the ID's exist in both tables. If you want to have it where the ID doesn't exist in the other table, then you need the OUTER JOIN.

Replace INNER JOIN with LEFT OUTER JOIN and I think you'll get the results you want.
0
 
LVL 8

Assisted Solution

by:gpizzuto
gpizzuto earned 250 total points
ID: 37773476
you should use the LEFT OUTER JOIN command:

Select products.id, products.desc, products.details, brands.BrandName, products.IDBrand  from products LEFT OUTER JOIN Brands on brands.BrandID = products.BrandID

Is not a good idea to put the 0 value where there is no Brand. Use instead the NULL value:
better to set up a RELATION between the 2 tables (much safer)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Expert Comment

by:deighton
ID: 37775354
to give the 0 when there is no brand record

Select products.id, products.desc, products.details, brands.BrandName, COALESCE(products.IDBrand, 0)  from products LEFT join Brands on brands.BrandID = products.BrandID
0
 
LVL 8

Expert Comment

by:gpizzuto
ID: 37776074
If you use 0 value you cannot create a relation between the tables (also in future) ...
0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 37777621
Thank you!  I appreciate the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
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.
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

739 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