Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - Combining OUTER and INNER joins

Tags:

sql-server

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:

  1. First do a inner join between A and F to get a set (this may be a null set)
  2. Then do a outer join with the recordset in (1) with D
like image 605
Bob Smith Avatar asked Jan 23 '09 16:01

Bob Smith


People also ask

Can I use inner join and outer join together?

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.

What is the reason behind using inner join and outer join?

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.

What is inner join and outer join in SQL Server with examples?

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

What does (+) mean in SQL joins?

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.


2 Answers

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
like image 158
kristof Avatar answered Oct 17 '22 04:10

kristof


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 
like image 31
gamov Avatar answered Oct 17 '22 02:10

gamov