Difference between Join and union in SQL

SQLSQLiteDatabase

Union is a set operator that can be used to combine the result set of two different SELECT statement. In the union number of columns and data type should be the same. 

It has two types −

  • Union
  • Union All 

On the other hand ,In a Relational database, tables are associated with each other and we used foreign key to maintain relationships between tables. We used join clause to retrieve data from associated tables. The join condition indicates how column in each table are matched against each other. 

There are two types of joins clause in SQL 

  • Inner join 
  • Outer join

Sr. No.
Key
Join
UNION

1

Basic 

It can be used to retrieve matched records between both tables or more tables 

It can be used to combine the result set of two different SELECT statement.

2

        Data type 

Result set can have different types of data types 

Data type should be same as the  result set of each select statement

3

Duplicate 

It doesn't remove duplicate data.

It removes duplicate rows between the various select statements.

Example of UNION

SELECT columnlist
FROM tableA
UNION
SELECT columnlist
FROM tableB

Example of JOIN

SELECT columnlist
FROM TableA
INNER JOIN
TableB ON join condition


raja
Published on 21-Jan-2020 14:16:18
Advertisements