Sunday 14 July 2013

Join and Union

Difference between Join and Union


Join
By using joins, you can retrieve data from two or more tables based on logical relationships between the tables.
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.


Difference between Join and Union


Join


Union

Only primary key & foreign key column & data types of both tables must be same


All column & data types of both tables must be same

Selects columns from 2 or more tables


Selects rows from two or more 
tables

Generally used to combine rows from multiple tables


Generally used to merge two or more tables

SELECT a.Column1, b.Column2 FROM TableA a INNER JOIN TableB b ON a.id = b.id

SELECT 1 AS [Column1], 2 AS [Column2]
UNION
SELECT 3 AS [Column1], 4 AS [Column2]




EmoticonEmoticon