What is the difference between JOIN and UNION in SQL?

Asked

Viewed 675 times

1

What’s the difference between JOIN and UNION in SQL? I thought a query made with either of the two would result in the same table, but I’m taking a course at Datacamp where the teacher says these two clauses don’t do exactly the same thing.

As the Joins are explained using the Venn diagram, I thought that for example a FULL JOIN would do the same as the clause UNION or even the clasp INETERSECT would do the same as the clause INNER JOIN.

There is even a difference in the results of a query using one or the other?

  • 1

    The result of a JOIN is one line containing the selected fields of the tables involved. The result of a UNION is a ensemble lines each coming from one of the tables involved.

1 answer

1


JOIN is used to link tables through a common value.
UNION is used to join two or more queries and bring the whole result together

Yes, the results are quite different, the goal of one is to relate the tables, the other join results.

Take this example: insert link description here

If only to UNION without relating the tables will have a Cartesian result, ie each item of each table x each item of the other, which is different from the result of a JOIN

Browser other questions tagged

You are not signed in. Login or sign up in order to post.