Stats

77 Contributors: 5 Wednesday, September 14, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

UNION

Syntax

  • UNION DISTINCT -- dedups after combining the SELECTs
  • UNION ALL -- non dedup (faster)
  • UNION -- the default is DISTINCT
  • SELECT ... UNION SELECT ... -- is OK, but ambiguous on ORDER BY
  • ( SELECT ... ) UNION ( SELECT ... ) ORDER BY ... -- resolves the ambiguity

Remarks

UNION does not use multiple CPUs.

UNION always* involves a temp table to collect the results. *As of 5.7.3 / MariaDB 10.1, some forms of UNION deliver the results without using a tmp table (hence, faster).

Related Examples