Question

I was going through union and union all logic and trying examples. What has puzzled me is why is it necessary to have same number of columns in both the tables to perform a union or union all operation?

Forgive me if my question's silly, but i couldn't get the exact answer anywhere. And conceptually thinking, as long as one common column is present, merging two tables should be easy right?(like a join operation). But this is not the case, and I want to know why?

Was it helpful?

Solution

JOIN operations do NOT require the same number of columns be selected in both tables. UNION operations are different that joins. Think of it as two separate lists of data that can be "pasted" together in one big block. You can't have columns that don't match.

Another way to look at it is a JOIN does this:

TableA.Col1, TableA.Col2 .... TableB.Col1, TableB.Col2

A UNION does this:

TableA.Col1, TableA.Col2
TableB.Col1, TableB.Col2

JOINS add columns to rows, UNIONS adds more rows to existing rows. That's why they must "match".

Matt

OTHER TIPS

Join Operation combines columns from two tables.

Where as Union and Union all combines two results sets, so In order to combine two results you need to have same number of columns with compatible data types.

In real world e.g. In order to play a game of cricket you need 11 players in both team, 7 in one team and 11 in opposite team not allowed.

Lets say you have EMPTable with columns and values as below: id, name, address, salary, DOB 1, 'SAM', '2 Merck Ln', 100000, '08/18/1980'

IF you want to UNION with only the column name (lets say value is 'TOBY'), it means you have to default other values to NULL (a smart software or db can implicitly do it for you), which in essence translates to below (to prevent the integrity of a relational table) ->

1, 'SAM', '2 Merck Ln', 100000, '08/18/1980'
UNION
NULL,'TOBY', NULL, NULL, NULL

A "union" by definition is a merger of (different) values of THE same class or type.

You need to research the difference between UNION and JOIN.

Basically, you use UNION when you want to get records from one source (table, view, group of tables, etc) and combine those results with records from another source. They have to have the same columns to get a common set of results.

You use JOIN when you want to join records from one source with another source. There are several types of JOINs (INNER, LEFT, RIGHT, etc) depending on your needs. When using JOINs, you can specify whichever columns you'd like.

Good luck.

join are basically used when we want to get data from two or more tables, and for this there is no need to fetch same number of columns, consider a situation where by using normalization concepts we have divided the table into two parts,

emp [eid, ename,edob,esal] emp_info [eid,emob_no]

here i we want to know name and mobile no. of all employees then we will use the concept of join, because here information needed by us can't be provided by same table.

so we will use..

SELECT E.ENAME,EI.EMOB_NO FROM EMP E, EMP_INFO EI WHERE E.EID = EI.EID AND LOWER(E.ENAME)='john' ;

now consider about situation where we want to find employees those are having saving account and loan account in a bank.. here we want to find common tuples from two tables results. for this we will use set operations. [ intersection ]

for set operations 2 conditions MUST BE satisfied.

  1. same no. of attributes must be fetches from each table.

  2. domain of each attribute must be same of compatible to the higher one...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top