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"
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 have a db2 query and I realized today I need to expand that query.
My table's already pretty complicated with joins, so I don't really want to add a union query. I want to do a full outer join.
At the moment, it's showing
SELECT
a.id
,a.city
,a.state
,case when a.thing = b.thing then a.thing else b.thing end
,sum( case when c.thing = 'thing' then 1 else 0 end)
,b.id
,b.name
FROM
a
INNER JOIN b -- I want to change this to FULL OUTER JOIN
ON a.id = b.id
LEFT JOIN c
ON a.id = c.id
LEFT JOIN (d
INNER JOIN e
ON d.id = e.id
)
WHERE
--logic
GROUP BY
--for the aggregate functions
ORDER BY
--logic
Can someone tell me when I try to do a full outer join, it says 'Full Outer Join is not supported for this query'? And how would I overcome that?
I think it's because of the other left joins.
It may not be able to combine the outer join with the left joins. You may have to make the outer join a subquery (also added some missing aliases and an ON clause):
SELECT
ab.a_id
,ab.city
,ab.state
,ab.thing
,sum( case when c.thing = 'thing' then 1 else 0 end)
,ab.b_id
,ab.name
FROM
(
SELECT
a.id a_id
,a.city
,a.state
,case when a.thing = b.thing then a.thing else b.thing end thing
,b.id b_id
,b.name
FROM
a
FULL OUTER JOIN b
ON a.id = b.id
) ab
LEFT JOIN c
ON ab.id = c.id
LEFT JOIN (d
INNER JOIN e
ON d.id = e.id
) f
ON ...
WHERE
--logic
GROUP BY
--for the aggregate functions
ORDER BY
--logic
In this query other than 5 join tables i am trying to use 6th table "Days" to compare value with three tables in joins. but it give me error that i cant use subquery in joins.
select
a.ID, a.Name, a.AMT, b.Address, c.Date, c.Pay, d.Check
from
Table1 a
left outer join Table2 b on a.ID = b.ID
left outer join Table3 c on a.ID = c.ID and c.Date= (select Derived_date from Days where TODAY_DATE = TO_DATE(SYSDATE, 'YYYY/MM/DD'))
left outer join Table4 d on a.ID = d.ID and d.Date= (select Derived_date from Days where TODAY_DATE = TO_DATE(SYSDATE, 'YYYY/MM/DD'))
left outer join Table5 e on a.ID = e.ID and e.Date= (select Derived_date from Days where TODAY_DATE = TO_DATE(SYSDATE, 'YYYY/MM/DD'))
Trying to use a subselect in an ON clause isn't going to work to well. You'd need to JOIN back to it like you would any other table. Since your subselect is the same for every single JOIN, I'd put that in a (temp?) table first so you can JOIN to it normally and not have to SELECT the same data three times.
CREATE TABLE Derived_Dates AS SELECT Derived_date FROM Days WHERE TODAY_DATE = TO_DATE(SYSDATE, 'YYYY/MM/DD')
SELECT a.ID, a.Name, a.AMT, b.Address, c.Date, c.Pay, d.Check
FROM Table1 a LEFT OUTER JOIN Table2 b on a.ID = b.ID
LEFT OUTER JOIN Table3 c ON a.ID = c.ID
LEFT OUTER JOIN Table4 d ON a.ID = d.ID
LEFT OUTER JOIN Table5 e ON a.ID = e.ID
INNER JOIN Dervied_date dt ON c.Date = dt.Derived_date
AND d.Date = dt.Derived_date
AND e.Date = dt.Derived_date
Here's how you can do it with your subselect:
SELECT a.ID, a.Name, a.AMT, b.Address, c.Date, c.Pay, d.Check
FROM Table1 a LEFT OUTER JOIN Table2 b on a.ID = b.ID
LEFT OUTER JOIN Table3 c ON a.ID = c.ID
LEFT OUTER JOIN Table4 d ON a.ID = d.ID
LEFT OUTER JOIN Table5 e ON a.ID = e.ID
INNER JOIN (SELECT Derived_date FROM Days WHERE TODAY_DATE = TO_DATE(SYSDATE, 'YYYY/MM/DD')) dt ON c.Date = dt.Derived_date
AND d.Date = dt.Derived_date
AND e.Date = dt.Derived_date
Instead of JOINing back to your derived dates, you could also just use a WHERE clause. You have some options, and you might want to make some changes for your particular implementation, but this is more or less how I'd approach this.
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
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