Skip to content

RIGHT_JOIN

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

Right Join

RIGHT_JOIN(a,b, pred): rightJoin 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.

Sample output

For the following input:

const employee = {
  name: "employee",
  rows: [
    {id: 1, name: "Josh", department_id: 1},
    {id: 2, name: "Jane", department_id: 2},
    {id: 3, name: "Ruth", department_id: 3},
    {id: 4, name: "Elliot", department_id: 1},
    {id: 5, name: "Michael", department_id: null},
    {id: 6, name: "Garth", department_id: null},
  ],
};

const department = {
  name: "department",
  rows: [
    {id: 1, name: "Sales"},
    {id: 2, name: "Engineering"},
    {id: 3, name: "Management"},
    {id: 4, name: "Consultants"},
  ],
};

innerJoin

Equivalent SQL: SELECT * FROM employee JOIN department ON employee.department_id = department.id;

Code:
INNER_JOIN(employee, department,
  (c) => c["employee.department_id"] === c["department.id"]
);
Result:
-- Inner join employee, department on department id
-- Equivalent SQL: SELECT * FROM employee JOIN department ON employee.department_id = department.id;
employee.id,employee.name,employee.department_id,department.id,department.name
1,Josh,1,1,Sales
2,Jane,2,2,Engineering
3,Ruth,3,3,Management
4,Elliot,1,1,Sales

leftJoin

Equivalent SQL: SELECT * FROM employee LEFT JOIN department ON employee.department_id = department.id;

Code:
LEFT_JOIN(employee, department,
  (c) => c["employee.department_id"] === c["department.id"]
);
Result:
-- Left join employee, department on department id --
-- Equivalent SQL: SELECT * FROM employee LEFT JOIN department ON employee.department_id = department.id;
employee.id,employee.name,employee.department_id,department.id,department.name
1,Josh,1,1,Sales
2,Jane,2,2,Engineering
3,Ruth,3,3,Management
4,Elliot,1,1,Sales
5,Michael,,,
6,Garth,,,

rightJoin

Equivalent SQL: SELECT * FROM employee RIGHT JOIN department ON employee.department_id = department.id;

Code:
RIGHT_JOIN(employee, department,
  (c) => c["employee.department_id"] === c["department.id"]
);
Result:
-- Right outer join on department id --
-- Equivalent SQL: SELECT * FROM employee RIGHT JOIN department ON employee.department_id = department.id;
department.id,department.name,employee.id,employee.name,employee.department_id
1,Sales,1,Josh,1
1,Sales,4,Elliot,1
2,Engineering,2,Jane,2
3,Management,3,Ruth,3
4,Consultants,,,

Clone this wiki locally