Multiple LEFT OUTER JOIN on multiple tables - sql

I would like to convert the following Oracle SQL query syntax (Use LEFT OUTER JOIN instead of (+)):
SELECT *
FROM TABLEA A, TABLEB B, TABLEC C, TABLED D
WHERE MY_COL = #col_val
AND A.X = B.X(+)
AND B.Y = C.Y(+)
AND D.Z=A.Z
Here is what I tried so far:
SELECT *
FROM TABLEA A, TABLEB B, TABLEC C, TABLED D
LEFT OUTER JOIN TABLEA A ON A.X = B.X
LEFT OUTER JOIN TABLEC C ON B.Y = C.Y
WHERE MY_COL = #col_val
AND D.Z = A.Z;
But I get the error :
"ORA-00904: "B"."X" : invalid identifier"

The join on D is an inner join, the rest are left outer joins:
SELECT *
FROM TABLEA A JOIN
TABLED D
ON D.Z = A.Z LEFT JOIN
TABLEB B
ON A.X = B.X LEFT JOIN
TABLEC C
ON B.Y = C.Y
WHERE MY_COL = #col_val;
I always start chains of joins with inner joins followed by the left outer join. I never use right join, and full join rather rarely. The inner joins define the rows in the result set, so they come first.

You don't should mix explicit and implicit sintax
SELECT *
FROM TABLEA A
INNER JOIN TABLEL L ON L.Z = A.Z
LEFT OUTER JOIN TABLEB B ON A.X = B.X
LEFT OUTER JOIN TABLEC C ON B.Y = C.Y
WHERE A.MY_COL = #col_val
you should use inner join (or join) for TABLEL

Try this:
SELECT *
FROM TABLEA A
LEFT OUTER JOIN TABLEB B ON A.X = B.X
LEFT OUTER JOIN TABLEC C ON B.Y = C.Y
INNER JOIN TABLED D ON D.Z = A.Z
WHERE MY_COL = #col_val
TABLEB and TABLEC goes in LEFT OUTER JOIN (you have used +), instead TABLED goes in INNER JOIN

Related

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...

order of join in hive

I have a query which involves a left join followed by a join. I want to make sure the left join is done first. The left join comes before the join nin my query, is this enough? This is how the join looks like
select * from
(select *....) A
left join
(select *...) B
on A.a = B.a
left join
C
on A.f = C.f
I cannot see the JOIN in your code, only two LEFT JOIN statements.
However, if you have something like this:
select * from
(select *....) A
left join
(select *...) B
on A.a = B.a
join
C
on A.f = C.f
and you want to make sure the LEFT JOIN is executed first, you can move this LEFT JOIN to a sub-query:
select *
from (
select * from (
(select *....) A
left join
(select *...) B
on A.a = B.a
)
) D
join
C
on D.f = C.f

joining 3 table sql

table A(a_id)
table B (b_id)
table c(c_id , b_id, a_id);
select a.*, b.* , c*
from c join b
i'm confused from here ???
Try this -
SELECT * FROM A
INNER JOIN C
ON A.a_id = C.a_id
INNER JOIN B
ON B.b_id = C.b_id;
SELECT a.*, b.*, c*
FROM c
LEFT JOIN b USING (b_id)
LEFT JOIN a USING (a_id);
Try this
SELECT A.* from tableA A
LEFT JOIN tableC C ON C.a_id = A.a_id
LEFT JOIN tableB B ON C.b_id = B.b_id
You could use INNER, LEFT, RIGHT, FULL... JOINS in following:
SELECT *
FROM A
LEFT JOIN B ON A.a_id = B.b_id
LEFT JOIN C ON B.b_id = C.c_id

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

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