How to write a transitive sql join in DB2? - sql

I want something like the following.
SELECT fewCols, aColFromNewTbl FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B ON A.ID = B.ID
LEFT OUTER JOIN TABLE_C AS C ON A.ID = C.ID
INNER JOIN A_NEW_TABLE AS NEWTBL ON NEWTBL.ID = B.ID;
Somehow I'm not able to achieve this functionality. Actually above query is suppose to join A with NEWTBL, but I'm joining it with B, which is already joined with A. For my results I want them to come exclusively from the join of NEWTBL and B. I don't know how I can get desired results?

Probably you need this:
SELECT fewCols, aColFromNewTbl
FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B
INNER JOIN A_NEW_TABLE AS NEWTBL
ON NEWTBL.ID = B.ID
ON A.ID = B.ID
LEFT OUTER JOIN TABLE_C AS C
ON A.ID = C.ID;

Related

Right join vs left join, which table is left vs right?

Given
select *
from a
left join b
on a.id = b.id
is table a left and table b right?
Would that be equivalent to
Select *
from a
right join b
on b.id = a.id
because I switched left and right while flipping the ON clause? Or is a still left because it came first and b is right because it's the thing we're joining?
Thank you.
No. "left" and "right" refer to the ordering of the tables in the FROM clause. So these are equivalent:
select *
from a left join
b
on a.id = b.id
select *
from b right join
a
on a.id = b.id
These two on clauses do exactly the same thing:
on a.id = b.id
on b.id = a.id
They do not affect the results at all.

issue in sql join query formation

I have two tables Say A and B. A is master table and B is child table, from which I need values as below.
select A.Id, A.Name, B.Path from A,B where A.Id=B.Id
Now, I want to add column of 3rd table which is child of table 'B', say C i.e. C.File.
The value of C.File will be null if C.SubId=B.SubId is false else will return value when condition becomes true.
This is the exact definition of a left join:
SELECT a.id, b.name, b.path, c.file
FROM a
JOIN b ON a.id = b.id
LEFT JOIN c ON b.subid = c.subid
You need to LEFT JOIN your third table from what I can gather.
SELECT A.Id, A.Name, B.Path, C.file
FROM tableA a
INNER JOIN tableB b ON a.id = b.id
LEFT JOIN tableC c ON b.subid = c.subid
Simply Join all the three tables using INNER JOIN
select A.Id, A.Name, B.Path ,C.File
FROM A
INNER JOIN B
ON A.Id=B.Id
INNER JOIN C
ON C.SubId=B.SubId

Oracle Sql left join return null value on condition.

Hi I have a left join like
Select * from tables a
Left join tableB b on b.id = a.id and b.name ='avc';
This query also return record were b.name is null..
Could someone plz explain the reason.
You need to use an INNER JOIN to avoid nulls.
SELECT * FROM tables a
INNER JOIN tableB b
ON b.id = a.id and b.name ='avc';
Check this for more information about joins.

Right join with a where clause

I found a query like this in code:
SELECT *
FROM a
RIGHT JOIN b ON a.id = b.id
WHERE a.id = b.id
Is it basically the same as an inner join on a.id = b.id?
Yes, this is basically the same as an inner join.
The where clause will fail when there are no matches, because the value a.id will be NULL.

Joining many tables together using key from first table for all joins?

I would like to do something like this but can't get it to work:
SELECT A.*,B.*,C.* FROM tableA A LEFT JOIN
B ON A.ID = B.ID
C ON A.ID = C.ID
I.e. I need to use a field from the first table for all joins as tableB and tableC don't
has any fields to link them together.
Can this be done?
you missed a join !
SELECT A.*,B.*,C.* FROM tableA A
LEFT JOIN
B ON A.ID = B.ID
left join
C ON A.ID = C.ID
Try this one -
SELECT *
FROM dbo.tableA A
LEFT JOIN dbo.B ON A.ID = B.ID
LEFT JOIN dbo.C ON A.ID = C.ID