Oracle SQL Left Join using multiple tables - sql

To be honest I am not sure how to layout this question, but I tried my best.
I am trying to join multiple tables. TableA has got no with TableE and TableF.
Observe the area in query where I tried to highlight with **
when it comes to TableE I joined with TableF which is further below. How to re-write this?
SELECT
a.NAME,b.ID,c.CODE
FROM SCHEMA.TABLEA A
INNER JOIN SCHEMA.TABLEB b
ON A.A_ID = B.B_ID
INNER JOIN SCHEMA.TABLEC C
ON A.A_ID = C.C_ID
LEFT JOIN SCHEMA.TABLED D
ON A.A_ID = D.D_ID
**LEFT JOIN SCHEMA.TABLEE E
ON D.D_ID = F.F_ID
LEFT JOIN SCHEMA.TABLEF F
ON E.E_ID = F.F_ID**

Since you stated that table f is realted to table d and table e is related to table f i think you just need to change the order of join as follow
SELECT
a.NAME,b.ID,c.CODE
FROM SCHEMA.TABLEA A
INNER JOIN SCHEMA.TABLEB b
ON A.A_ID = B.B_ID
INNER JOIN SCHEMA.TABLEC C
ON A.A_ID = C.C_ID
LEFT JOIN SCHEMA.TABLED D
ON A.A_ID = D.D_ID
LEFT JOIN SCHEMA.TABLEF F
ON D.D_ID = F.F_ID
LEFT JOIN SCHEMA.TABLEE E
ON E.E_ID = F.F_ID

Related

Why do these two queries return different results? Trying to replace select from multiple tables with joins

I'm trying to rewrite a select from multiple tables into a series of joins. I thought my solution would be equivalent but it's not returning the same data. An ideas? Here's the code:
/* without joins:*/
SELECT
A.column3,
D.column6,
E.column2,
I.column2,
E.column3,
F.column1,
G.column2
FROM table1 A, table2 B, table3 C, table4 D, table5 E, table6 F, table7 G table8 H
LEFT OUTER JOIN table9 I ON H.ID = I.ID
WHERE
A.ID = C.ID
B.ID = C.ID
D.ID = B.ID
D.ID = E.ID
E.ID = F.ID
F.ID = G.ID
E.ID = H.ID
AND H.ISDELETED<>'T'
AND D.ISDELETED<> 'T'
/* Here's my attempt to turn it into at series of joins: */
SELECT
D.column3,
E.column6,
I.column2,
E.column2,
F.column3,
G.column1
FROM table1 A
JOIN table3 C ON A.ID = C.ID
JOIN table2 B ON B.ID = C.ID
JOIN table4 D ON D.ID = B.ID
JOIN table5 E ON D.ID = E.ID
JOIN table6 F ON E.ID = F.ID
JOIN table7 G ON F.ID = G.ID
JOIN table8 H ON E.ID = H.ID
LEFT OUTER JOIN table9 I ON H.ID = I.ID
WHERE G.ISDELETED<>'T'
AND E.ISDELETED<> 'T'
Change your query with the JOIN's to this and see if it matches up with your initial query:
SELECT
A.column3,
D.column6,
E.column2,
I.column2,
E.column3,
F.column1,
G.column2
FROM table1 A
INNER JOIN table2 B ON A.ID = C.ID
INNER JOIN table3 C ON B.ID = C.ID
INNER JOIN table4 D ON D.ID = B.ID
INNER JOIN table5 E ON D.ID = E.ID
INNER JOIN table6 F ON E.ID = F.ID
INNER JOIN table7 G ON F.ID = G.ID
INNER JOIN table8 H ON E.ID = H.ID
LEFT OUTER JOIN table9 I ON H.ID = I.ID
WHERE
AND H.ISDELETED<>'T'
AND D.ISDELETED<> 'T'
Your aliases do not match between the two queries which may throw off the results of the JOIN.
Also, in your first query your WHERE clause is:
AND table8.ISDELETED<>'T'
AND table4.ISDELETED<> 'T'
In your bottom query, your WHERE clause is:
AND table7.ISDELETED<>'T'
AND table5.ISDELETED<> 'T'
Which may also lead to difference results between the two.

How to join a total of 6 tables?

I want join 6 tables:
Table A join Table B
Table A join Table C
Table A Join Table D
Table D Join Table E
Table E Join Table F
But I can't run it:
SELECT
A.ID, A.Name, A.SName,
B.tel, B.status,
C.ComCode, C.Comdes,
D.Type,
E.Des, E.AreaDes,
F.AreaID
FROM
((((Table A
INNER JOIN
Table B ON A.ID = B.ID)
INNER JOIN
Table C ON A.Concode = C.Comcode)
INNER JOIN
Table D ON A.Type = D.Type)
INNER JOIN
Table E ON D.Des = E.Des)
INNER JOIN
Table F ON E.AreaID = F.AreaID
Get rid of your parentheses and table and it should be vailid SQL, so
SELECT ...
FROM a INNER JOIN b ON a.a = b.a
INNER JOIN c ON b.b = c.a
...
Try this :
SELECT
A.ID, A.Name, A.SName,
B.tel, B.status,
C.ComCode, C.Comdes,
D.Type,
E.Des, E.AreaDes,
F.AreaID
FROM Table A
INNER JOIN Table B ON A.ID = B.ID
INNER JOIN Table C ON A.Concode = C.Comcode
INNER JOIN Table D ON A.Type = D.Type
INNER JOIN Table E ON D.Des = E.Des
INNER JOIN Table F ON E.AreaID = F.AreaID
It will help if you tell us what you database server is e.g., SQL Server, Oracle, MySQL, etc. When you say, "...I can't run it..." are you getting an error? If so, can you share that.
I ditched your parenthesis...
SELECT
A.ID,
A.Name,
A.SName,
B.tel,
B.status,
C.ComCode,
C.Comdes,
D.Type,
E.Des,
E.AreaDes,
F.AreaID
FROM
TableA A INNER JOIN TableB B ON A.ID = B.ID
INNER JOIN TableC C ON A.Concode = C.Comcode
INNER JOIN TableD D ON A.Type = D.Type
INNER JOIN TableE E ON D.Des = E.Des
INNER JOIN TableF F ON E.AreaID = F.AreaID
For clarity, I adjusted your table names (so it was clear what was Table A vs. Table B).

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

Adding more joins to a select

I am having some trouble trying to add 2 more joins to a select. The bellow works for me:
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
LEFT JOIN TABLE3 C ON A.REQUESTED_BY = C.USER_NAME
LEFT JOIN TABLE3 D ON A.COORDINATOR = D.USER_NAME
INNER JOIN TABLE4 E ON A.ID = E.PARENT_ID
INNER JOIN TABLE5 F ON E.ID = F.ID
But I need to get more information, so I tried something like this (added the last 2 rows):
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
LEFT JOIN TABLE3 C ON A.REQUESTED_BY = C.USER_NAME
LEFT JOIN TABLE3 D ON A.COORDINATOR = D.USER_NAME
INNER JOIN TABLE4 E ON A.ID = E.PARENT_ID
INNER JOIN TABLE5 F ON E.ID = F.ID
INNER JOIN TABLE6 G ON A.ID = B.ID
LEFT JOIN TABLE3 H ON G.COORDINATOR = H.USER_NAME
And this isn't working like it should.
Question: How can I add the last two joins to make the select works? Thanks.
You're not actually joining to TABLE6 (G) anywhere. I would think that this join:
INNER JOIN TABLE6 G ON A.ID = B.ID
should be something like this instead:
INNER JOIN TABLE6 G ON A.ID = G.ID
And as a side note, I hope you're using table aliases that are more meaningful than A, B, C, etc. in your real code. ;-)

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