?
Solved

SQL Inner Join Question

Posted on 2012-03-27
6
Medium Priority
?
366 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 1000 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 1000 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
Independent Software Vendors: 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!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

741 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