I have 3 tables and I have to inner join Table A with Table B but a left outer between Table A and Table C.
Can I combine outer and inner join in the same query? I can nest queries and attain the desired result but I am unable to do both the joins in the same query. It appears that in other SQL languages, the order of joining is important. Is this the case in SQL Server as well?
Ok, here's the scenario.
Consider 3 tables. Table A, Table F, Table D.
I will need the recordset to contain all rows in D irrespective of whether it exists in F (after it's inner joined with A). So, a outer join comes to mind. What I would need is:
You either have to use two LEFT joins and filter out the records you don't want, or alternatively use a View to scope the INNER JOIN. Show activity on this post. Yes you can do both is the same query, and yes the order is important.
The biggest difference between an INNER JOIN and an OUTER JOIN is that the inner join will keep only the information from both tables that's related to each other (in the resulting table). An Outer Join, on the other hand, will also keep information that is not related to the other table in the resulting table.
(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.
The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.
If I understand correctly you want something like this:
select
*
from
a
left outer join c
inner join b on c.bID = b.ID
on a.cID = c.ID
For my case, I needed to put table aliases for my query to work properly:
SELECT * FROM ("purchased_items" p1
INNER JOIN "purchase_orders" po1 ON (po1."id" = p1."purchase_order_id")) AS p4
LEFT OUTER JOIN (purchased_items p2
INNER JOIN "purchase_orders" po2 ON (po2."id" = p2."purchase_order_id")) AS p5
ON (p4.item_variant_id = p5.item_variant_id AND p4.delivery_date < p5.delivery_date)
WHERE p5.delivery_date IS NULL AND p4.delivered <> 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With