MySQL Wildcards

MySQL Wildcards

MySQL Aliases

MySQL Aliases

MySQL Data Types

MySQL Data Types

MySQL Interview Questions

MySQL Interview Questions and Answers


MySQL RIGHT JOIN

In MySQL, RIGHT JOIN is used to fetch all records from RIGHT side table (tablename2) and only matching records from left side table (tablename1). It appears immediately after the FROM clause.

MySQL RIGHT JOIN Syntax

To fetch all records from RIGHT side table (tablename1) and only matching records from left side table (tablename2), use the following syntax:

Syntax

SELECT col1, col2,....
FROM tablename1
RIGHT JOIN tablename2 ON tablename1.col1 = tablename2.col1;

Parameters:

tablename1 & tablename2: The table name from which you want to fetch records.

col1: The column or field name that will be return.

MySQL RIGHT JOIN example

The following MySQL, fetching all "banks" fields value from "tblbankheadquartersandaddress" table and only matching record from "accountName" field from "tblbankdemataccountcharges" table using RIGHT JOIN, both have matching column values in tables :

Example

SELECT B.banks,A.accountName
FROM tblbankdemataccountcharges A
RIGHT JOIN tblbankheadquartersandaddress B
ON A.bankId = B.pk LIMIT 6;

Note:

In the above example, we are fetching all "banks" fields value from "tblbankheadquartersandaddress" table and only matching record from "accountName" field from "tblbankdemataccountcharges" table using RIGHT JOIN, both have matching column values in tables.

You can use MySQL Command Line Client to fetch all records from right table and only matching record from left side table where the join condition is satisfied. It will look like this: