Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Noob Question - Multiple Tables

Author  Topic 

rnelsch
Starting Member

31 Posts

Posted - 2012-06-14 : 17:57:19
I'm trying to create a query from multiple tables and I know how the Inner Joins work, but for some reason in my query the data is only pulling from 1 table and not both. For example, some records show on both Table A and Table B, but Table B will have records that are not included on Table A. When I create the inner joins it seems to limit only records that on Table A. Is there some type of syntax or query language I can use to make sure it includes all the data from both tables outside of Inner Joins?

Table A - Holds all Jobs (whether they are created from a phone call, sales call, booked directly, etc)
Table B - Hold only Jobs that are created from Phone Calls

When I add Table B, it limits the return to only data found on Table B. I need all the data to show, but only 1 field from Table B, and when I add that field it limits the number of records to only those found on Table B.

Yes, I'm a noob, I know but I'm the only one in the office with even the slightest idea of how to write a SQL query. I figured an Inner Join would return data from both tables, is it possible I'm missing a field in my Joins?

Ryan A Nelsch

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-06-14 : 18:22:20
You'll need to use an OUTER JOIN instead of an INNER JOIN. INNER JOIN will only show the matches. If you need them ones that don't match from both tables, then you'd use a FULL OUTER JOIN.

Whether or not I'm answering your question right, I don't know. If I'm not, please post your query and show us some sample data to illustrate the issue.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rnelsch
Starting Member

31 Posts

Posted - 2012-06-15 : 09:59:34
That definitely helps, can I use both Inner and Outer joins?

Ryan A Nelsch
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-06-15 : 12:49:25
Yes.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rnelsch
Starting Member

31 Posts

Posted - 2012-06-15 : 12:55:11
Here is what I have so far, without Inner Joins. I only used the Left join for Fields I want to see that are shared on both tables

Select Job.JobID,
Job.ProjectID,
Job.LocationID,
Job.JobClass,
Job.JobType,
Job.Department,
Job.SchedDate,
Job.TotalTicket,
PhoneCall.LeadSource,
PhoneCall.IsNewLocation
From Job
Left Join PhoneCall
On Job.JobID = PhoneCall.ForJobID and Job.LocationID = PhoneCall.LocationID and Job.JobClass = PhoneCall.JobClass and Job.Department = PhoneCall.Department
and Job.JobType = PhoneCall.JobType


FYI - There are more fields that are shared in each table, do I need to create a Join for each field or can I do it only for the ones I want to view?
Ryan A Nelsch
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-06-15 : 13:13:47
I can't really answer your question without seeing sample data. You typically only join on the primary key/foreign key columns. And that's typically an ID column.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rnelsch
Starting Member

31 Posts

Posted - 2012-06-15 : 13:20:53
I gotcha. Now what about joining three tables. How would I use a Outer join with three tables?

Ryan A Nelsch
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-06-15 : 14:13:44
It's the same code as an inner join, use outer instead of inner.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -