I have Parent table A.
A has few child tables such as B,C,D,E,F,G
The child tables are not linked to each other. They are only linked to A.
A has a key Id which is used as foreign key in all the child tables.
What should be the best way to join these tables so I can create a single view on this?
Since a parent may have a child row in some of those tables you must use LEFT OUTER JOIN.
LEFT OUTER JOIN joins two tables returning all the rows of the LEFT table, in this case A and all the matches from the other tables. When there is no match it will return NULL in the corresponding columns of the tables that there was no match.
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.Id = B.ParentID
LEFT OUTER JOIN C
ON A.Id = C.ParentID
LEFT OUTER JOIN P
ON C.Id = P.ParentID
LEFT OUTER JOIN Q
ON C.Id = Q.ParentID
LEFT OUTER JOIN D
ON A.Id = D.ParentID
LEFT OUTER JOIN E
ON A.Id = E.ParentID
LEFT OUTER JOIN F
ON A.Id = F.ParentID
LEFT OUTER JOIN X
ON F.Id = X.ParentID
LEFT OUTER JOIN Y
ON F.Id = Y.ParentID
LEFT OUTER JOIN G
ON A.Id = G.ParentID
EDIT
I have added a way to add subchilds. I have intented them more just to make them obvious in a visual representation. But beware...if this lead to subchildren have other subchildren etc maybe your structure is not optimal.
select <wanted columns>
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
Related
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.
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. ;-)
From everything I have learned about LEFT OUTER JOIN, the table you want to be nullable should be on the right side of the equals symbol. If this is the case, why do both of these queries return the same result:
SELECT *
FROM employees e
LEFT JOIN cars c ON c.employeeID=e.id AND c.name='Honda City'
WHERE c.id IS NULL
ORDER BY e.id ASC;
SELECT *
FROM employees e
LEFT JOIN cars c ON e.id=c.employeeID AND c.name='Honda City'
WHERE c.id IS NULL
ORDER BY e.id ASC;
Demo: http://sqlfiddle.com/#!15/46d00/2
Q1 uses A LEFT JOIN B ON A.id = B.id
Q2 uses A LEFT JOIN B ON B.id = A.id
You have changed the LHS and RHS of the items being compared in the ON clause, but the LEFT join is talking about which TABLE is on the left.
So to see a difference you would make Q2 use "B LEFT JOIN A ON A.id = B.id"
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;
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