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
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
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
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 |
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]
You can achieve it using union and Please refer the below link
http://www.w3schools.com/sql/sql_union.asp