A RIGHT JOIN query is a RIGHT OUTER JOIN query that returns all rows in the right-side table, and does not return unmatched rows in the left-side table.
Background information
An OUTER JOIN query uses a comparison operator to compare the data in two tables. The join results include the rows that meet the join conditions and those that do not meet the join conditions.
OUTER JOIN queries include FULL JOIN queries, LEFT JOIN queries, and RIGHT JOIN queries. An OUTER JOIN query returns all the rows that meet the join conditions. In addition, it returns unused rows of one table and fills NULL in the corresponding positions in the other table
This topic describes how to use RIGHT JOIN queries. For more information about FULL JOIN queries and LEFT JOIN queries, see FULL JOIN queries and LEFT JOIN queries.
Syntax
Generally, an OUTER JOIN statement contains a left-side table and a right-side table. The leftmost table in the JOIN clause is the left-side table, and the rightmost table in the JOIN clause is the right-side table.
The syntax is as follows:
SELECT select_list FROM table_name1 RIGHT JOIN table_name2 ON join_condition
[ WHERE query_condition ]
[ ORDER BY column_list ];
Here, table_name1 is the left-side table and table_name2 is the right-side table.
Examples
Create a table and insert proper data into the table.
obclient [SYS]> CREATE TABLE tbl_a(id NUMBER NOT NULL PRIMARY KEY, name VARCHAR2(50));
Query OK, 0 rows affected
obclient [SYS]> CREATE TABLE tbl_b(num NUMBER NOT NULL PRIMARY KEY, value NUMBER);
Query OK, 0 rows affected
obclient [SYS]> INSERT INTO tbl_a VALUES(1,'ab'),(2,'cd'),(3,'ef'),(4,'gh');
Query OK, 6 rows affected
Records: 6 Duplicates: 0 Warnings: 0
obclient [SYS]> INSERT INTO tbl_b VALUES(1,1001),(3,1003),(5,1005);
Query OK, 4 rows affected
Records: 4 Duplicates: 0 Warnings: 0
Simple RIGHT JOIN queries
To use a JOIN clause to return data that meets the join and filter conditions and also to return data from the right-side table that meets the filter condition specified for the right-side table but does not meet the join condition, you can use RIGHT JOIN. In a RIGHT JOIN query, the rows in the right-side table matching no rows in the left-side table are returned with the NULL value.
Example 1: Execute a RIGHT JOIN query on the tbl_a and tbl_b tables and obtain the returned results.
obclient [SYS]> SELECT * FROM tbl_a;
+----+------+
| ID | NAME |
+----+------+
| 1 | ab |
| 2 | cd |
| 3 | ef |
| 4 | gh |
+----+------+
4 rows in set
obclient [SYS]> SELECT * FROM tbl_b;
+-----+-------+
| NUM | VALUE |
+-----+-------+
| 1 | 1001 |
| 3 | 1003 |
| 5 | 1005 |
+-----+-------+
3 rows in set
obclient> SELECT tbl_a.id, tbl_a.name, tbl_b.num, tbl_b.value FROM tbl_a RIGHT JOIN tbl_b ON tbl_a.id=tbl_b.num;
+------+------+--------+-------+
| ID | NAME | NUM | VALUE |
+------+------+--------+-------+
| 1 | ab | 1 | 1001 |
| 3 | ef | 3 | 1003 |
| NULL | NULL | 5 | 1005 |
+------+------+--------+-------+
3 rows in set
RIGHT JOIN queries with a WHERE clause
You can use a RIGHT JOIN query to obtain the join result and then use the WHERE clause to filter the result.
Example 2: Execute a RIGHT JOIN query on the tbl_a and tbl_b tables and return the data that meets the value=1003 condition in the tbl_b table.
obclient> SELECT t1.id, t1.name, t2.id, t2.name FROM t1
LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id IS NOT NULL;
+----+------+------+------+
| ID | NAME | ID | NAME |
+----+------+------+------+
| 1 | A1 | 1 | B2 |
| 2 | B1 | NULL | NULL |
| 4 | D1 | NULL | NULL |
| 6 | F1 | 6 | F2 |
| 8 | H1 | NULL | NULL |
| 10 | J1 | NULL | NULL |
+----+------+------+------+
6 rows in set