SQL Inner Joins inside Outer Joins; Nesting Alters Results - sql

I'm a bit surprised that these two queries give different results:
First Query:
SELECT a.number, a.name , b.*
FROM Atable a
LEFT OUTER JOIN Btable b
JOIN Ctable c ON c.number = b.number
ON b.number = a.number
ORDER BY a.number;
Second Query:
SELECT a.number, a.name , b.*
FROM Atable a
LEFT OUTER JOIN Btable b ON b.number = a.number
JOIN Ctable c ON c.number = b.number
ORDER BY a.number
My expectation is that both of these would return the results that the first query does. The first query returns every row from TableA; however, unexpectedly, the second row only returns results from TableA if they also exist in TableC.
Why does the join from C to B restrict TableA in the second query but not in the first query?
Thanks!

Your first query, with parens to clarify how it is parsed:
SELECT a.number, a.name , b.*
FROM Atable a LEFT OUTER JOIN
(Btable b JOIN
Ctable c
ON c.number = b.number
) ON b.number = a.number
ORDER BY a.number;
Having two on clauses in a row is confusing, so the parentheses help. This makes it clear that you are keeping all rows from the first table.
The second query is:
SELECT a.number, a.name , b.*
FROM (Atable a LEFT OUTER JOIN
Btable b
ON b.number = a.number
) JOIN
Ctable c
ON c.number = b.number
ORDER BY a.number;
You are inner joining the result of the first join. Hence, only rows that match will go to the result set.
When you are doing multiple joins, I recommend using left join for all the joins. Mixing inner and outer joins can lead to confusion.

Related

SQL Join - If it doesn't find any, try another parameters

Let's suppose we have the following query:
SELECT *
FROM tableA a
LEFT JOIN tableB b ON b.number = a.number
AND b.name = a.name
AND b.place = a.place
If that LEFT JOIN doesn't find nothing, I need to change the ON parameters to something like:
SELECT *
FROM tableA a
LEFT JOIN tableB b ON b.number = a.number
AND b.person = a.person
That second LEFT JOIN only needs to run if the first one doesn't return nothing. How can I achieve that behaviour?
I have already tried with an OR statement, but doesn't work because the first LEFT JOIN always need to be checked first, and not at the same time that the second.
Perhaps the simplest method is union all:
SELECT a.*, b.*
FROM tableA a JOIN
tableB b
ON b.number = a.number AND
b.name = a.name AND
b.place = a.place
UNION ALL
SELECT a.*, b.*
FROM tableA a JOIN
tableB b
ON b.number = a.number AND
b.person = a.person
WHERE NOT EXISTS (SELECT 1
FROM tableB b
WHERE b.number = a.number AND
b.name = a.name AND
b.place = a.place
);

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.

SQL summations with multiple outer joins

I have tables a, b, c, and d whereby:
There are 0 or more b rows for each a row
There are 0 or more c rows for each a row
There are 0 or more d rows for each a row
If I try a query like the following:
SELECT a.id, SUM(b.debit), SUM(c.credit), SUM(d.other)
FROM a
LEFT JOIN b on a.id = b.a_id
LEFT JOIN c on a.id = c.a_id
LEFT JOIN d on a.id = d.a_id
GROUP BY a.id
I notice that I have created a cartesian product and therefore my sums are incorrect (much too large).
I see that there are other SO questions and answers, however I'm still not grasping how I can accomplish what I want to do in a single query. Is it possible in SQL to write a query which aggregates all of the following data:
SELECT a.id, SUM(b.debit)
FROM a
LEFT JOIN b on a.id = b.a_id
GROUP BY a.id
SELECT a.id, SUM(c.credit)
FROM a
LEFT JOIN c on a.id = c.a_id
GROUP BY a.id
SELECT a.id, SUM(d.other)
FROM a
LEFT JOIN d on a.id = d.a_id
GROUP BY a.id
in a single query?
Your analysis is correct. Unrelated JOIN create cartesian products.
You have to do the sums separately and then do a final addition. This is doable in one query and you have several options for that:
Sub-requests in your SELECT: SELECT a.id, (SELECT SUM(b.debit) FROM b WHERE b.a_id = a.id) + ...
CROSS APPLY with a similar query as the first bullet then SELECT a.id, b_sum + c_sum + d_sum
UNION ALL as you suggested with an outer SUM and GROUP BY on top of that.
LEFT JOIN to similar subqueries as above.
And probably more... The performance of the various solutions might be slightly different depending on how many rows in A you want to select.
SELECT a.ID, debit, credit, other
FROM a
LEFT JOIN (SELECT a_id, SUM(b.debit) as debit
FROM b
GROUP BY a_id) b ON a.ID = b.a_id
LEFT JOIN (SELECT a_id, SUM(b.credit) as credit
FROM c
GROUP BY a_id) c ON a.ID = c.a_id
LEFT JOIN (SELECT a_id, SUM(b.other) as other
FROM d
GROUP BY a_id) d ON a.ID = d.a_id
Can also be done with correlated subqueries:
SELECT a.id
, (SELECT SUM(debit) FROM b WHERE a.id = b.a_id)
, (SELECT SUM(credit) FROM c WHERE a.id = c.a_id)
, (SELECT SUM(other) FROM d WHERE a.id = d.a_id)
FROM a

Syntax for multiple joins in sql

Working on Oracle: I am attempting to do an inner self join, with a where clause, then take that result and do a left outer join on it:
(select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id
Somehow I am syntactically challenged and can't make this work. Can't seem to find the right syntax anywhere.
Just the put the where clause at the end. The database will get it right:
select *
from table1 A
inner join table1 B on A.id = B.id
left join table2 D on D.id = A.id
where A.id is not null
In this case, we can take advantage of the logical transitive property for your id column joins and where clause.
Your second join needs to be joined to a query add a select * from at the beginning
select * from (select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id