Friday, 4 May 2018

Right And Right Outer Join With examples in Sql Server


 A RIGHT OUTER JOIN is one of the JOIN operations that allow you to specify a JOIN clause. It preserves the unmatched rows from the second (right) table, joining them with a NULL in the shape of the first (left) table. A LEFT OUTER JOIN B is equivalent to B RIGHT OUTER JOIN A, with the columns in a different order.
This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.


declare @table1 table  (ID INT, NAME VARCHAR(10))

INSERT INTO @table1 (ID, NAME)
SELECT 1,'First'
UNION ALL
SELECT 2,'Second'
UNION ALL
SELECT 3,'Third'
UNION ALL
SELECT 4,'Fourth'
UNION ALL
SELECT 5,'Fifth'
union all
SELECT 6,'Sixth'

declare @table2 table (ID INT, NAME VARCHAR(10))

INSERT INTO @table2 (ID, NAME)
SELECT 1,'First'
UNION ALL
SELECT 2,'Second'
UNION ALL
SELECT 3,'Third'
UNION ALL
SELECT 6,'Sixth'
UNION ALL
SELECT 7,'Seventh'
UNION ALL
SELECT 8,'Eighth'

SELECT * FROM @table1

SELECT * FROM @Table2


--
SELECT t1.ID, t1.NAME,t2.ID, t2.NAME
FROM @table1 t1
RIGHT JOIN @Table2 t2 ON t1.ID = t2.ID




No comments:

Post a Comment

How to create a new database in Microsoft

Sql server This article explains how to create a new database on the following versions of Microsoft SQL Server: § Microsoft SQL Server ...