×

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 EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL SOUNDEX() function

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

3 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 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 Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL FORMAT() function

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

2 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL RPAD() function

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

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.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute 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 Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 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 MONTHNAME() function

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

3 minutes read.