CodesJava

Easy learning with example program codes

Right Outer Join in MariaDB

RIGHT JOIN
The MariaDB Right Outer Join query returns all the rows from the Right table for the specified fields. For the Left table, it only returns those rows where the join condition is met.

Syntax:

SELECT expr_1, expr_2, ... expr_n  
FROM table_1   
RIGHT JOIN table_2  
ON join_predicate;

Example:
Players Table:

ID	NAME	         SPORTS
1	Sachin	         Cricket
2	Dhoni	         Cricket
3	Sunil	         Football
4	Srikanth	 Badminton

Trainers Table:

TRAINER_ID	TRAINER_NAME	TRAINER_SPORTS
101	        Bond	        Football
102	        Smith	        Badminton
103	        Brand	        Boxing

Query:

SELECT players.id, players.name, trainers.trainer_id  
FROM players  
RIGHT JOIN trainers  
ON players.sports = trainers.trainer_sports;

Output:

ID	NAME	        TRAINER_ID
3	Sunil	        101
4	Srikanth	102
NULL	NULL	        103

Explanation:
The PLAYERS and the TRAINERS are the already existing tables. All the records for the selected fields from the TRAINERS table will be added to the result set. And only those records from the selected fields of the PLAYERS table will be added to the result set which are satisfying the join conditions.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status