Saturday, December 10, 2022

SQL Right Join

Right join in SQL helps combine data from two tables. One can begin writing a query along the lines of select data from a table. The query is then followed by 'right join' keyword and the second table name whose data needs to be combined with the first table. Then there is an on clause which joins the data between the two tables based on common values between similar fields in the two tables.

In the below example we have a table named 'name'.


Our second table would be a table named 'age'.


The empID (employee ID) column is the common factor between the two tables. We will thus perform a join on these columns. The output should be a combination of the two tables. Hence the output must contain the columns 'empID', 'name' and 'age'. We will leverage the concept of right join as shown below to achieve our goal. Note that in a right join all records from the right hand table are returned while matching values are returned by the left hand side table.



No comments: