Set Operators

Description

Set operators are used to combine two input relations into a single one. Spark SQL supports three types of set operators:

  • EXCEPT or MINUS
  • INTERSECT
  • UNION

Note that input relations must have the same number of columns and compatible data types for the respective columns.

EXCEPT

EXCEPT and EXCEPT ALL return the rows that are found in one relation but not the other. EXCEPT (alternatively, EXCEPT DISTINCT) takes only distinct rows while EXCEPT ALL does not remove duplicates from the result rows. Note that MINUS is an alias for EXCEPT.

Syntax

  1. [ ( ] relation [ ) ] EXCEPT | MINUS [ ALL | DISTINCT ] [ ( ] relation [ ) ]

Examples

  1. -- Use number1 and number2 tables to demonstrate set operators in this page.
  2. SELECT * FROM number1;
  3. +---+
  4. | c|
  5. +---+
  6. | 3|
  7. | 1|
  8. | 2|
  9. | 2|
  10. | 3|
  11. | 4|
  12. +---+
  13. SELECT * FROM number2;
  14. +---+
  15. | c|
  16. +---+
  17. | 5|
  18. | 1|
  19. | 2|
  20. | 2|
  21. +---+
  22. SELECT c FROM number1 EXCEPT SELECT c FROM number2;
  23. +---+
  24. | c|
  25. +---+
  26. | 3|
  27. | 4|
  28. +---+
  29. SELECT c FROM number1 MINUS SELECT c FROM number2;
  30. +---+
  31. | c|
  32. +---+
  33. | 3|
  34. | 4|
  35. +---+
  36. SELECT c FROM number1 EXCEPT ALL (SELECT c FROM number2);
  37. +---+
  38. | c|
  39. +---+
  40. | 3|
  41. | 3|
  42. | 4|
  43. +---+
  44. SELECT c FROM number1 MINUS ALL (SELECT c FROM number2);
  45. +---+
  46. | c|
  47. +---+
  48. | 3|
  49. | 3|
  50. | 4|
  51. +---+

INTERSECT

INTERSECT and INTERSECT ALL return the rows that are found in both relations. INTERSECT (alternatively, INTERSECT DISTINCT) takes only distinct rows while INTERSECT ALL does not remove duplicates from the result rows.

Syntax

  1. [ ( ] relation [ ) ] INTERSECT [ ALL | DISTINCT ] [ ( ] relation [ ) ]

Examples

  1. (SELECT c FROM number1) INTERSECT (SELECT c FROM number2);
  2. +---+
  3. | c|
  4. +---+
  5. | 1|
  6. | 2|
  7. +---+
  8. (SELECT c FROM number1) INTERSECT DISTINCT (SELECT c FROM number2);
  9. +---+
  10. | c|
  11. +---+
  12. | 1|
  13. | 2|
  14. +---+
  15. (SELECT c FROM number1) INTERSECT ALL (SELECT c FROM number2);
  16. +---+
  17. | c|
  18. +---+
  19. | 1|
  20. | 2|
  21. | 2|
  22. +---+

UNION

UNION and UNION ALL return the rows that are found in either relation. UNION (alternatively, UNION DISTINCT) takes only distinct rows while UNION ALL does not remove duplicates from the result rows.

Syntax

  1. [ ( ] relation [ ) ] UNION [ ALL | DISTINCT ] [ ( ] relation [ ) ]

Examples

  1. (SELECT c FROM number1) UNION (SELECT c FROM number2);
  2. +---+
  3. | c|
  4. +---+
  5. | 1|
  6. | 3|
  7. | 5|
  8. | 4|
  9. | 2|
  10. +---+
  11. (SELECT c FROM number1) UNION DISTINCT (SELECT c FROM number2);
  12. +---+
  13. | c|
  14. +---+
  15. | 1|
  16. | 3|
  17. | 5|
  18. | 4|
  19. | 2|
  20. +---+
  21. SELECT c FROM number1 UNION ALL (SELECT c FROM number2);
  22. +---+
  23. | c|
  24. +---+
  25. | 3|
  26. | 1|
  27. | 2|
  28. | 2|
  29. | 3|
  30. | 4|
  31. | 5|
  32. | 1|
  33. | 2|
  34. | 2|
  35. +---+