MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.