MySQL MySQL Unions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any MySQL Question?