In relational database, the basic functionality of Join and Union is to combined two ore more tables. Everyone follow their own mechanism. This article describes a comparison between Join and Union. Summary of the article:
- What is Join?
- What is Union?
- Join VS Union
What is Join?
By using joins, you can retrieve data from two or more tables based on logical relationships between the tables.
What is Union?
The UNION set operator is used for combining data from two tables which have columns with the same data type. When a UNION is performed the data from both tables will be collected in a single column having the same data type.
Join VS Union
The Join and Union is a basic operations in SQL. Though their basic functionality is same, but have some difference. The Difference between Join and Union is given bellow:
Join
- Only primary key & foreign key column & data types of both tables must be same.
- Selects columns from 2 or more tables.
- Generally used to combine rows from multiple tables.
- SELECT a.Column1, b.Column2 FROM TableA a INNER JOIN TableB b ON a.id = b.id
Union
- All column & data types of both tables must be same.
- Selects rows from two or more tables.
- Generally used to merge two or more tables.
- SELECT 3 AS [Column1], 4 AS [Column2]
Don’t be confused on SQL JOIN clause and SQL Union operator. We need clear idea about Join and Union. Otherwise it will hamper our application. We can miss use them.