SQL OUTER JOIN Syntax with filters for both tables

What is the correct syntax for the first "WHERE statement" in this Full Join statement?  (for MSSQL)  

SELECT * FROM TableA WHERE col1 IS not NULL
FULL OUTER JOIN (Select * from TableB WHERE col2 > 2) T2
ON TableA.orderID = T2.orderID

Open in new window

eeyoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
This is the correct syntax.  Haven't checked it to make sure it runs but the general syntax is:

SELECT * FROM TableA 
FULL OUTER JOIN (Select * from TableB WHERE col2 > 2) T2
ON TableA.orderID = T2.orderID
WHERE col1 IS not NULL

Open in new window


Select columns from tab1 full outer join tab2 on tab1.col1 = tab2.col2 where <conditions> is the general format.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You could also use this, with is more like your original query (but I would not do it that way):
select *
from (SELECT * FROM TableA WHERE col1 IS not NULL) TableA
full join (Select * from TableB WHERE col2 > 2) T2
on TableA.orderID = T2.orderID

Open in new window

Usually it is better to have all conditions in the main query:
select *
from TableA
full join TableB
on TableA.orderID = TableB.OrderID
where TableA.col1 is not null
and TableB.col2 > 2

Open in new window

0
eeyoAuthor Commented:
select *
from TableA
full join TableB
on TableA.orderID = TableB.OrderID
where TableA.col1 is not null
and TableB.col2 > 2

Open in new window

For this version, if a row doesn't exist in TableA, but it exists in TableB, will "TableA.col1 is not null" filter out the row in TableB (because it is a full join)?  If the row exists only in TableB, it needs to be returned with "col1" being null.  Sorry, I can't test it right now because I won't have access to the DB for a while.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Steve WalesSenior Database AdministratorCommented:
That sounds about right.  If you don't want those rows, why not just do a left join instead of a full join ?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You are correct, any condition in WHERE can contradict to the OUTER JOIN purpose. Above is effectively an Inner Join. The correct translation is done by moving the WHERE into the ON, if you really need to have unmatched rows on both sides:
select *
from TableA
full join TableB
on TableA.orderID = TableB.OrderID
and TableA.col1 is not null
and TableB.col2 > 2

Open in new window

Though I ask myself why you exclude col1 - if there is a row in TableA with matching orderID, but col1 is null, that row will not "match", and TableA cols all be null.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eeyoAuthor Commented:
Putting the condition in the ON section should work.
Though I ask myself why you exclude col1 - if there is a row in TableA with matching orderID, but col1 is null, that row will not "match", and TableA cols all be null.
In this report, we need to know when there isn't a matching row in TableA when it exists in TableB (and col1 should show up as Null).  It's a somewhat strange report but the above SQL statement will help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.