Joins in SQL Server

Inner Joins

It match records together based on one or more common fields, as do most JOINS but inner joins return only records where there are matches for whatever fields you have said are to be used for join.

Join Two Tables

Table_1 : Product_Master

product_master

Table_2 : Sales_Order_Detaisl

sales_order_details

Query:

select a.product_no,a.decscription from product_master a
inner join sales_order_details b on a.product_no = b.product_no

Multiple Inner Joins

select a.product_no,a.decscription,d.name from product_master a
inner join sales_order_details b on a.product_no = b.product_no
inner join sales_order c on b.s_order_no = c.s_order_no
inner join client_master d on c.client_no = d.client_no
where d.name=’Ivan Bayross’

Outer Joins

Syntax: Select * from left_table_name <LEFT or RIGHT>  OUTER  JOIN right_table_name on <condition>

Example

select a.product_no from product_master a
left outer join sales_order_details b on
a.product_no = b.product_no

It will display all records of left, it does not important to find matches with right table. And it does not display right table records which are not match found

CROSS Join

A cross join is differ from other joins in that there is no ON operator and that it joins every record on one side of the JOIN with every record on the other side of the JOIN.

Syntax : Select * from left_table_name cross join right_table_name

It will display possibility of matching records in both tables. Suppose LEFT table have 3 records and right table 5 then Cross join display 3*5=15 records, match possibilities.

Share