Stats

46 Contributors: 2 Thursday, September 15, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

MySQL Unions

Syntax

  • SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
  • SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2;
  • SELECT column_name(s) FROM table1 WHERE col_name="XYZ" UNION ALL SELECT column_name(s) FROM table2 WHERE col_name="XYZ";

Remarks

UNION DISTINCT is the same as UNION; it is slower than UNION ALL because of a de-duplicating pass. A good practice is to always spell out DISTINCT or ALL, thereby signaling that you thought about which to do.

Related Examples