Solved

SQL Inner Join Question

Posted on 2012-03-27
6
360 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

695 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