RIGHT JOIN

Returns records when we have the corresponding foreign key in the table after RIGHT JOIN. This is true even if they do not match the following table. On the right table we have complete and additional information from the left table, only if they exist.

Overview of join types:

 

Structure:
SELECT column_name FROM table_name1
RIGHT JOIN table_name2 ON table_name1.column_name = table_name2.column_name;

 

 

Example1:

We search from two tables: Friends table, Colors table.

Table Color:

COLORID COLOR
1 blue
2 green
3 pink
4 yellow

 

Table Friends:

FRIENDID FRIEND COLORID
1 Frank 1
2 Helena 2
3 Julia -
4 Thomas 4

 

We search from two tables: Friends table, Colors table.

We are looking for records that belong together. How do I know that records belong together? I know this by the COLORID field, which is located in both tables. COLORID represents a number expressing color. This means that each number has a different color. COLORID is called a „foreign key“.

SELECT Friends.Friend, Friends.ColorID, Colors.Color
FROM Friends
RIGHT JOIN Colors
ON Friends.ColorID = Colors.ColorID;

 

The result will be the following output because we are interested in all the Colors table records. We are only interested in records from the Friends table if they are paired with the Colors table.

FRIEND COLORID COLOR
Frank 1 blue
Helena 2 green
Thomas 4 yellow
- - pink

 

Leave a Comment

Your email address will not be published. Required fields are marked *