LEFT JOIN
The MariaDB Left Outer Join query returns all the rows from the Left table for the specified fields. For the Right table, it only returns those rows where the join condition is met.
Syntax:
SELECT expr_1, expr_2, ... expr_n FROM table_1 LEFT JOIN table_2 ON join_predicate;
Example:
Players Table:
ID NAME SPORTS 1 Sachin Cricket 2 Dhoni Cricket 3 Sunil Football 4 Srikanth Badminton 5 Mary Boxing
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 LEFT JOIN trainers ON players.sports = trainers.trainer_sports; |
Output:
ID NAME TRAINER_ID 1 Sachin NULL 2 Dhoni NULL 3 Sunil 101 4 Srikanth 102 5 Mary 103
Explanation:
The PLAYERS and the TRAINERS are the already existing tables. All the records for the selected fields from the PLAYERS table will be added to the result set. And only those records from the selected fields of the TRAINERS table will be added to the result set which are satisfying the join conditions.