What is the Difference between join and union ?

Answer : The union operator combines the results of two or more queries into a single result set…

Union

    • The union operator combines the results of two or more queries into a single result set. But no.of columns must match in both/all the queries (and also the order) which are used for union.
    • We can union two tables irrespective of common field.
    • The UNION set operator is used for combining data from two tables which have columns with the same datatype.
      • Union – Returns with no duplicate rows.
      • Union all – Retruns with duplicate rows (No. of rows returned = No. of rows in Query1 + No. of rows in Query 2).
Union unionall except intersect
  • We cannot use the union operator within a create view statement.
  • We cannot use the union operator on text and image columns.
Union in SQL

Syntax

[pastacode lang=”sql” manual=”SELECT%20expression1%2C%20expression2%2C%20…%20expression_n%0AFROM%20tables%0A%5BWHERE%20conditions%5D%0AUNION%0ASELECT%20expression1%2C%20expression2%2C%20…%20expression_n%0AFROM%20tables%0A%5BWHERE%20conditions%5D%3B” message=”” highlight=”” provider=”manual”/]

Join

  • Joins are used to extract information from more than one table based on the related column/coloums (PK and RFK) any no. of rows can be retrived based on matching colums.
  • We can join two tables by ‘join’ if they have common field.
  • A Join is used for displaying columns with the same or different names from different tables.
  • Different types of joins are Inner join=equi join=join , outer join (Right outer join/right join, Left outer join / left join), Cross join , and full outer join/outer join.
  • We can use join operator within a create view statement.
  • We can use the join operator on text and image columns.
Cross Join

Syntax

[pastacode lang=”sql” manual=”SELECT%20column-names%0AFROM%20table-name1%20JOIN%20table-name2%20%0AON%20column-name1%20%3D%20column-name2%0AWHERE%20condition” message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like