RIGHT JOIN and FULL JOIN - SQL Query

Consider the following tables Table1 and Table2.

Table1: Clients

C_IDClient_NameClient_Phone
1Alex9022222222
2Mark9033333333
3SAM9044444444
4John9055555555

Table2: Payment

C_IDTransaction_IDAmount
1101355
2102255
3103300

RIGHT JOIN or RIGHT OUTER JOIN

Q. Write a query to perform right join on table 'Clients' to display the column 'Clients_Name' from Table1 and 'Amount' from table2.

Answer:

Consider Table1 as Right and Table2 as left. The RIGHT JOIN returns the all possible rows from right table and shows null in left table.

right join

SELECT Clients.Client_Name, Payment.Amount
FROM Payment
RIGHT JOIN Clients
ON Payment.C_ID = Clients.C_ID


Output:

Client_NameAmount
Alex355
Mark255
SAM300
John

FULL JOIN or FULL OUTER JOIN

Q. Write a query to perform full outer join and display columns such as C_ID, Client_Name, Transaction_ID and Amount.

Answer:

full outer join

SELECT Clients.C_ID, Clients.Client_Name, Payment.Transaction_ID, Payment.Amount
FROM Clients
FULL JOIN Payment
ON Payment.C_ID = Clients.C_ID;


In the above query the FULL JOIN (OUTER JOIN) is performed on Table1. This type of join Combines the result  the all rows from left table and all rows from right table.

Output:

C_IDClient_NameTransaction_IDAmount
1Alex101355
2Mark102255
3SAM103300
4John