Oracle inner and outer join valid combinations - sql

Is it valid to combine Oracle inner and outer joins in the same query a la:
select b.col1, c.col2, sum(d.col1), sum(e.col1) from
a
inner join b on a.xxx = b.xxx
inner join c on a.yyy = c.yyy
left join d on b.aaa = d.aaa and c.bbb = d.bbb
left join e on b.aaa = e.bbb and c.aaa = e.bbb
group by b.col1, c.col2

Yes, you can combine inner, left and right joins the way you deem fit in the query. Just make sure that you understand the implications of inner, left and outer joins. Couple of blogs describe joins nicely: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ and http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

The outer join nulls are ignored in your sum(d.col1) and sum(e.col1) values.
Why SUM(null) is not 0 in Oracle?
If alternate default values are needed for your null outer join results, consider NVL and similar.
Sum columns with null values in oracle

Please try this, All non aggregated columns should be in group by clause.
select b.col1, c.col2, sum(d.col1), sum(e.col1) from
a
inner join b on a.xxx = b.xxx
inner join c on a.yyy = c.yyy
left join d on b.aaa = d.aaa and c.bbb = d.bbb
left join e on b.aaa = e.bbb and c.aaa = e.bbb
group by b.col1, c.col2

Related

BiqQuery Left Outer with an OR condition Error

Ran into the BigQuery limitation that you can't use an OR on a JOIN. Runs fine in Oracle.
Looking for some hints on how to accomplish this in another matter.
The last left outer join produces this error.
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
select [col list]
from tablea a
left outer join tableb b on a.id = b.ID
left outer join tabled d on a.id = d.iid
left outer join tablee e on b.id = e.pid and b.cid = e.id
left outer join tablef f on d.runitid = f.id or e.runitid = f.id
Due to additional join dependencies down the stack I switched it to a JOIN which allows the OR condition. More verification of results will be done.

Nested Table Joins

I have C table as Main Table, which is joining left join with E table on col3 and which is joining left join with F table on col4.
(
FROM A
LEFT OUTER JOIN B ON A.col1 = B.col1
LEFT OUTER JOIN C ON A.col2 = C.col2
LEFT OUTER JOIN E ON A.col3 = E.col3
LEFT OUTER JOIN F ON A.col4 = F.col4
)temptab1
Which needs to left join with
(
FROM C ON
LEFT OUTER JOIN E ON C.col3 = E.col3
LEFT OUTER JOIN F ON C.col4 = F.col4
)temptab2
Now, should i join temptab1 to temptab2 on (C.Col3 = temptab2.Col3 OR C.Col4 = temptab2.Col4) ? OR
should i join temptab1 to temptab2 on (C.Col3 = temptab2.Col3 AND C.Col4 = temptab2.Col4) ?
IF i have to use OR clause, it is hindering performance of the query a lot...
I tried using union clause instead of OR clause. Still, query performance is not improving...
Please suggest better query.
The temptab1 is all rows of A. The temptab2 is all rows of C. And the temptab1 is LEFT JOIN with temptab2, which results in all rows of A.
You may use CASE statements to identify the matching values across A, B, C, D, E, F and remove the Join with temptab2.
Apologies, could not comment...

Outer join between three tables causing Oracle ORA-01417 error

We're trying to run an outer join between table A on both tables B and C but get the error:
ORA-01417: a table may be outer joined to at most one other table
How can we get this to work?
Query:
select a.xxx, a.yyy, b.col1, c.col1 from a, b, c
where
a.xxx = b.xxx (+) and
a.yyy = b.yyy (+) and
a.xxx = c.xxx (+) and
a.yyy = c.yyy (+)
Use proper explicit join syntax. I think the following is probably what you want to do:
select a.xxx, a.yyy, b.col1, c.col1
from a left join
b
on a.xxx = b.xxx and a.yyy = b.yyy left join
c
on a.xxx = c.xxx and a.yyy = c.yyy;
You could try:
select a.xxx, y.xxx, b.col1, c.col2
from a
left join b on a.xxx = b.xxx and a.yyy = b.yyy
left join c on a.xxx = c.xxx and a.yyy = c.yyy
Please refrain from using comma in the from clause and use the JOIN clause instead.
Try using this:
select a.xxx, a.yyy, b.col1, c.col1
from a LEFT JOIN b ON a.xxx = b.xxx AND a.yyy = b.yyy
LEFT JOIN c ON a.xxx = c.xxx AND a.yyy = c.yyy

SQL Server Multiple LEFT JOIN, one-to-many

I am looking for a way to perform multiple joins from one source table to more than one table. Similar to the following:
SELECT a.NAME, b.address, c.phone
FROM tblname a
LEFT JOIN tbladdress b ON a.nid = b.nid
I also want to perform a left join on the Telephone table tblPhone at the same time:
tblname a left join tblPhone c on a.PID = c.PID
Try as I might I can't see how to put this into one query.
You can simply repeat your JOIN clauses as many times as is needed, e.g.:
SELECT a.NAME
,b.address
,c.phone
FROM tblname a
LEFT JOIN tbladdress b ON a.nid = b.nid
LEFT JOIN tblPhone c ON a.PID = c.PID
SELECT a.name, b.address, c.phone
FROM tblname a
left join tbladdress b on a.nid = b.nid
left join tblPhone c on a.PID = c.PID;
SELECT a.name, b.address, c.phone
FROM (tblname a
left join tbladdress b on a.nid = b.nid) c
left join tblPhone d on c.PID=d.PID

Cascading left outer joins

What is the correct syntax to perform an outer join with the following requirements:
A left outer join B on A.c1 = B.c1
B left outer join C on B.c2 = C.c2
A left outer join D on A.c1 = D.c1
So A, B, and C cascade and A and D cascade.
I know how to write the A->B->C but I don't know how to add D. I need scope or parenthesis or something.
this should work as you want:
SELECT
*
FROM A
left outer join B on A.c1 = B.c1
left outer join C on B.c2 = C.c2
left outer join D on A.c1 = D.c1
the DB engine looks at what your are joining to, not the order of the joins. D joins to A, and has nothing to do with B or C
The order in which you join doesn't matter, the database will build a result set of every combination of rows in all tables, limited by the on clause. For example, because 1=1 is always true, this would give you 1000 rows:
select *
from ten_row_table A
left join ten_row_table B on 1=1
left join ten_row_table C on 1=1
But this would give you 10 rows:
select *
from ten_row_table A
left join ten_row_table B on A.id = B.id
left join ten_row_table C on A.id = C.id
You can make complicated queries slightly more readable by indentation. We indent second and further dependencies by four spaces, like:
from A
left outer join B on A.c1 = B.c1
left outer join C on B.c2 = C.c2
left outer join D on C.c3 = D.c3
left outer join E on B.c2 = E.c2
left outer join F on A.c1 = F.c1