JOIN

Description

A SQL join is used to combine rows from two relations based on join criteria. The following section describes the overall join syntax and the sub-sections cover different types of joins along with examples.

Syntax

  1. relation { [ join_type ] JOIN [ LATERAL ] relation [ join_criteria ] | NATURAL join_type JOIN [ LATERAL ] relation }

Parameters

  • relation

    Specifies the relation to be joined.

  • join_type

    Specifies the join type.

    Syntax:

    [ INNER ] | CROSS | LEFT [ OUTER ] | [ LEFT ] SEMI | RIGHT [ OUTER ] | FULL [ OUTER ] | [ LEFT ] ANTI

  • join_criteria

    Specifies how the rows from one relation will be combined with the rows of another relation.

    Syntax: ON boolean_expression | USING ( column_name [ , ... ] )

    boolean_expression

    Specifies an expression with a return type of boolean.

Join Types

Inner Join

The inner join is the default join in Spark SQL. It selects rows that have matching values in both relations.

Syntax:

relation [ INNER ] JOIN relation [ join_criteria ]

Left Join

A left join returns all values from the left relation and the matched values from the right relation, or appends NULL if there is no match. It is also referred to as a left outer join.

Syntax:

relation LEFT [ OUTER ] JOIN relation [ join_criteria ]

Right Join

A right join returns all values from the right relation and the matched values from the left relation, or appends NULL if there is no match. It is also referred to as a right outer join.

Syntax:

relation RIGHT [ OUTER ] JOIN relation [ join_criteria ]

Full Join

A full join returns all values from both relations, appending NULL values on the side that does not have a match. It is also referred to as a full outer join.

Syntax:

relation FULL [ OUTER ] JOIN relation [ join_criteria ]

Cross Join

A cross join returns the Cartesian product of two relations.

Syntax:

relation CROSS JOIN relation [ join_criteria ]

Semi Join

A semi join returns values from the left side of the relation that has a match with the right. It is also referred to as a left semi join.

Syntax:

relation [ LEFT ] SEMI JOIN relation [ join_criteria ]

Anti Join

An anti join returns values from the left relation that has no match with the right. It is also referred to as a left anti join.

Syntax:

relation [ LEFT ] ANTI JOIN relation [ join_criteria ]

Examples

  1. -- Use employee and department tables to demonstrate different type of joins.
  2. SELECT * FROM employee;
  3. +---+-----+------+
  4. | id| name|deptno|
  5. +---+-----+------+
  6. |105|Chloe| 5|
  7. |103| Paul| 3|
  8. |101| John| 1|
  9. |102| Lisa| 2|
  10. |104| Evan| 4|
  11. |106| Amy| 6|
  12. +---+-----+------+
  13. SELECT * FROM department;
  14. +------+-----------+
  15. |deptno| deptname|
  16. +------+-----------+
  17. | 3|Engineering|
  18. | 2| Sales|
  19. | 1| Marketing|
  20. +------+-----------+
  21. -- Use employee and department tables to demonstrate inner join.
  22. SELECT id, name, employee.deptno, deptname
  23. FROM employee INNER JOIN department ON employee.deptno = department.deptno;
  24. +---+-----+------+-----------|
  25. | id| name|deptno| deptname|
  26. +---+-----+------+-----------|
  27. |103| Paul| 3|Engineering|
  28. |101| John| 1| Marketing|
  29. |102| Lisa| 2| Sales|
  30. +---+-----+------+-----------|
  31. -- Use employee and department tables to demonstrate left join.
  32. SELECT id, name, employee.deptno, deptname
  33. FROM employee LEFT JOIN department ON employee.deptno = department.deptno;
  34. +---+-----+------+-----------|
  35. | id| name|deptno| deptname|
  36. +---+-----+------+-----------|
  37. |105|Chloe| 5| NULL|
  38. |103| Paul| 3|Engineering|
  39. |101| John| 1| Marketing|
  40. |102| Lisa| 2| Sales|
  41. |104| Evan| 4| NULL|
  42. |106| Amy| 6| NULL|
  43. +---+-----+------+-----------|
  44. -- Use employee and department tables to demonstrate right join.
  45. SELECT id, name, employee.deptno, deptname
  46. FROM employee RIGHT JOIN department ON employee.deptno = department.deptno;
  47. +---+-----+------+-----------|
  48. | id| name|deptno| deptname|
  49. +---+-----+------+-----------|
  50. |103| Paul| 3|Engineering|
  51. |101| John| 1| Marketing|
  52. |102| Lisa| 2| Sales|
  53. +---+-----+------+-----------|
  54. -- Use employee and department tables to demonstrate full join.
  55. SELECT id, name, employee.deptno, deptname
  56. FROM employee FULL JOIN department ON employee.deptno = department.deptno;
  57. +---+-----+------+-----------|
  58. | id| name|deptno| deptname|
  59. +---+-----+------+-----------|
  60. |101| John| 1| Marketing|
  61. |106| Amy| 6| NULL|
  62. |103| Paul| 3|Engineering|
  63. |105|Chloe| 5| NULL|
  64. |104| Evan| 4| NULL|
  65. |102| Lisa| 2| Sales|
  66. +---+-----+------+-----------|
  67. -- Use employee and department tables to demonstrate cross join.
  68. SELECT id, name, employee.deptno, deptname FROM employee CROSS JOIN department;
  69. +---+-----+------+-----------|
  70. | id| name|deptno| deptname|
  71. +---+-----+------+-----------|
  72. |105|Chloe| 5|Engineering|
  73. |105|Chloe| 5| Marketing|
  74. |105|Chloe| 5| Sales|
  75. |103| Paul| 3|Engineering|
  76. |103| Paul| 3| Marketing|
  77. |103| Paul| 3| Sales|
  78. |101| John| 1|Engineering|
  79. |101| John| 1| Marketing|
  80. |101| John| 1| Sales|
  81. |102| Lisa| 2|Engineering|
  82. |102| Lisa| 2| Marketing|
  83. |102| Lisa| 2| Sales|
  84. |104| Evan| 4|Engineering|
  85. |104| Evan| 4| Marketing|
  86. |104| Evan| 4| Sales|
  87. |106| Amy| 4|Engineering|
  88. |106| Amy| 4| Marketing|
  89. |106| Amy| 4| Sales|
  90. +---+-----+------+-----------|
  91. -- Use employee and department tables to demonstrate semi join.
  92. SELECT * FROM employee SEMI JOIN department ON employee.deptno = department.deptno;
  93. +---+-----+------+
  94. | id| name|deptno|
  95. +---+-----+------+
  96. |103| Paul| 3|
  97. |101| John| 1|
  98. |102| Lisa| 2|
  99. +---+-----+------+
  100. -- Use employee and department tables to demonstrate anti join.
  101. SELECT * FROM employee ANTI JOIN department ON employee.deptno = department.deptno;
  102. +---+-----+------+
  103. | id| name|deptno|
  104. +---+-----+------+
  105. |105|Chloe| 5|
  106. |104| Evan| 4|
  107. |106| Amy| 6|
  108. +---+-----+------+