-4

I am in the process of learning MySQL right now and while I get how to do UNIONS and JOINS. However I'm not seeing the advantages of a UNION over any type of JOIN. They both combine results from tables but seems like you have to jump through more hoops to combine tables using UNION if they're not identical with their columns. Is there an advantage of using a UNION sometimes or is it just another command we can use?

sticky bit
  • 36,626
  • 12
  • 31
  • 42
  • 3
    They serve totally different purposes, there is no point in comparing them. `JOIN` follows relations (often linked though foreign keys), and `UNION` combine different results of the same structure into the same result set. – Alejandro Jul 07 '20 at 19:29
  • It's akin to the difference between plus and multiply – Strawberry Jul 07 '20 at 19:40
  • Mysql does not have UNPIVOT to unpivot you have to UNION(all) – P.Salmon Jul 08 '20 at 08:57
  • Does this answer your question? [What is the difference between JOIN and UNION?](https://stackoverflow.com/questions/905379/what-is-the-difference-between-join-and-union) – Peter O. Aug 18 '20 at 00:48

1 Answers1

0

UNION adds rows from multiple tables/views. Whereas join make the filters between rows from different related tables in a single sql statement.

Union: used to combine the result set of two different SELECT statement with same datatype of result set.

Join: used to retrieve matched records between 2 or more tables.

Please visit this link, it will help you to clear your doubts.

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
Krishan Kumar
  • 394
  • 4
  • 13