0

I am working on sql server 2008. I want to do something like this. I have two tables like this.

Table1

Id
1
2
3
4

Table2

Id
2
3
5
6

Output

1
2
3
4
5
6

4 Answers4

7

How about using UNION

Combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. The UNION operation is different from using joins that combine columns from two tables.

UNION ALL

Incorporates all rows into the results. This includes duplicates. If not specified, duplicate rows are removed.

SELECT Id
FROM Table1
UNION
SELECT Id
FROM Table2

Have a look at the below demo, which will also show you the difference between UNION and UNION ALL

SQL Fiddle DEMO

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
2

I think you are looking for UNION (Transact-SQL)

Combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union. The UNION operation is different from using joins that combine columns from two tables.

SELECT Id
FROM Table1

UNION

SELECT Id
FROM Table2

Just a tip, UNION removes all duplicate records, UNION ALL doesn't.

Here a DEMO.

| ID |
------
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

This can also be used if you have more than 1 column

SELECT coalesce(t1.ID, t2.ID) [Output]
FROM TABLE1 t1
FULL JOIN
TABLE2 t2 ON
t1.id = t2.id
ORDER BY [Output]
t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
0

You can achieve it using union and Please refer the below link

http://www.w3schools.com/sql/sql_union.asp
RAJESH KUMAR
  • 497
  • 4
  • 13