×

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with the left row. If the right table does not match the left table, the query returns a null value and when the right table matches with a left table then query return all row of the right table.

Syntax

The syntax of the right join shows below. This syntax works on two tables.

SELECT
Column1 data type constraint,
Column2 data type constraint,
Column3 data type constraint,
FROM table name1
RIGHT JOIN table2 ON condition1;

The syntax of the right join shows below. This syntax works on multiple tables.

SELECT
Column1 data type constraint,
Column2 data type constraint,
Column3 data type constraint,
FROM table name1
RIGHT JOIN table2 ON condition1
RIGHT JOIN table3 ON condition2;

Examples of the MySQL right join

1) Example: Right join with multiple table examples shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. Here, we have use "ON" condition.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> RIGHT JOIN subject s
    -> ON d.department_id = s.subject_id;

OUTPUT

RIGHT [OUTER] JOIN

The above output image shows the combined columns of the two tables. The "department_name" and "department_id" columns has in the department table. The "subject_name" and "students" columns have in the subject table.

2) Example: Right join with multiple table examples shows below.

Execute the below query to join two tables. Two tables join required columns using specified conditions. Here, the query selects entire columns of the table.

mysql> select *
    -> FROM department d
    -> RIGHT JOIN subject s
    -> ON d.department_id = s.subject_id;

OUTPUT

RIGHT [OUTER] JOIN

The above image shows all columns of both tables. For example, this query shows complete columns of the department and student tables.

3) Example: the Right join example shows below.

Execute the below query to join two tables. Two tables join all columns using the "RIGHT JOIN" query. This query uses ON and WHERE clause with RIGHT JOIN.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> RIGHT JOIN subject s
    -> ON d.department_id = s.subject_id
    -> WHERE subject_id < 3;

OUTPUT

RIGHT [OUTER] JOIN

The above image shows the combined columns of the two tables using the right join. Here, you can see four columns and two rows.

4) Example: Right joins with the "GROUP BY" clause example shows below.

Execute the below query to join two tables with required columns. Two tables join four columns using the "RIGHT JOIN" query. This query uses the "ON" and the "ORDER BY" clause with RIGHT JOIN.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> RIGHT JOIN subject s
    -> ON d.department_id = s.subject_id
    -> ORDER BY
    -> department_id DESC,
    -> subject_id DESC;

OUTPUT

RIGHT [OUTER] JOIN

The above image shows the combined columns of the two tables. Here, you can see four columns in descending order data.

5) Example: Right join with the "USING" clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. For example, the "RIGHT JOIN" query uses "USING" clause with the "subject_id" as a foreign key.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> RIGHT JOIN subject s
    -> USING (subject_id);

OUTPUT

RIGHT [OUTER] JOIN

The above output image displays combined columns of the department and subject tables. The "subject_id" column links two tables using a foreign key. The above table works right join query with the "USING" clause. Here, the table shows data using a common column or foreign key.

6) Example: Right joins with the "GROUP BY" clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. For example, the "RIGHT JOIN" query uses "GROUP BY" clause with the "subject_id" foreign key. It also uses AND logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> RIGHT JOIN subject s
    -> USING (subject_id)
    -> WHERE d.department_name = "Bachelor of Technology"
    -> GROUP BY subject_id;

OUTPUT

RIGHT [OUTER] JOIN

The above output image displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key.

7) Example: Right join with the multiple clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. For example, the "RIGHT JOIN" query uses "ORDER BY" and "using" clauses with the "subject_id" foreign key. It also uses OR logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> RIGHT JOIN subject s
    -> USING (subject_id)
    -> WHERE d.department_name = "Bachelor of Technology"
    -> OR  s.subject_name = "Computer Science"
    -> ORDER BY department_id DESC, subject_id DESC;

OUTPUT

RIGHT [OUTER] JOIN

The above output table displays combined columns of the department and subject tables. The "subject_id" column links two tables using a foreign key. The above table works right join query with the "USING" clause. Here, the table applies the right join query with the "ORDER BY" clause. The table data display in descending order.


Related Topics

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples. Introduction of MySQL RAND() function Basically, in MySQL the RAND() function is...

3 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.