SQL RIGHT JOIN

SQL RIGHT JOIN returns all the rows from the right table mentioned with the RIGHT JOIN clause. If there are no matching rows in left table then output rows will return NULL values.

Syntax

SELECT columnName(s)
FROM Table1 RIGHT JOIN Table2
ON Table1.columnName = Table2.columnName

Refer below image to understand RIGHT JOIN, here green part is output for RIGHT JOIN.

RightJoin

Here we will consider one example for RIGHT JOIN condition where we will use two tables named as SalesDetails and ClientDetails.

Table Name: SalesDetails

SalesDetails

Table Name: ClientDetails

ClientDetails

As you can see, first table has 8 records and second table has 10 records. From above two tables, we need columns OrderID and OrderDate from SalesDetails table and one column ClientName from ClientDetails table and sorted in descending order by OrderDate column. So to get the output from two tables we will use RIGHT JOIN query as given below.

SELECT SalesDetails.OrderID, ClientDetails.ClientName, SalesDetails.OrderDate
FROM SalesDetails RIGHT JOIN ClientDetails 
ON SalesDetails.ClientID = ClientDetails.ClientID
ORDER BY OrderDate DESC

RightJoin2

Kindly note the above highlighted records, in RIGHT JOIN all the records are returned from the table which is mentioned in right side and where there was no matching values it has returned NULL values.

Also Refer:

Reference: Manzoor Siddiqui [www.SQLServerLog.com]

You may also like

You may also like...

Leave a Reply