Skip to content

RIGHT_JOIN

Joshua Weinberg edited this page Nov 14, 2021 · 8 revisions

Right Join

Filtering a cross join to include rows which match a condition. Every row from the right table will be included at least once in the output even if it does not match the condition. In the non-matching case, nulls will be included for the left column values.

A right join operation is exactly the same as a left join except the right column values are always included and left column values are null when there is no match.

See LEFT_JOIN for a detailed description of how this works.

For example, using the following tables:

employee
+------+--------+-----------------+
| id   | name   | department_id   |
|------+--------+-----------------|
| 1    | Josh   | 1               |
| 2    | Ruth   | 2               |
| 3    | Greg   | 5               |
+------+--------+-----------------+

department
+------+-------------+
| id   | name        |
|------+-------------|
| 1    | Sales       |
| 2    | Marketing   |
| 3    | Engineering |
+------+-------------+

Note that the department table has a value (Engineering) which is not referenced by any employee.department_id.

LEFT_JOIN Implementation

RIGHT_JOIN(a,b, pred): right join takes two tables and a predicate. Result will be a table which includes the cross join of all rows which satisfy the predicate and which has no nulls in table b.

const RIGHT_JOIN = (a, b, pred) => {
  return LEFT_JOIN(b, a, pred);
};

The implementation just calls LEFT_JOIN with the tables reversed.

Clone this wiki locally