How do Join and Get back results in sql sever

Based on the following tables(Table1 and Table2) structure. I would like to get a Table3. How can I do this in Sql Server ?

Table1

ID Name
1   A
2   B
3   C

Table2

ID Name
1  D
2  E

Expected Output is

ID Name
1   D
2   E
3   C

Answers


You could use COALESCE with an outer join

SELECT t1.ID, 
       COALESCE(t2.Name, t1.Name)AS Name 
FROM   table1 t1 
       LEFT OUTER JOIN table2 t2 
               ON t1.ID = t2.ID

Demo (with insert into Table3)


Need Your Help

What is Java alternative for .NET's XmlSerializer

c# java xml port

I'm porting c# application to Java and don't know what it is doing in details. With what should I replace .Net's XmlSerializer in Java?

How come sql is slower straight after the DB has been created?

c# sql sql-server database-performance

I am doing some performance testing and have found that SQL seems to be performing much slower on a brand new db than on one which has been around a little while, and I would like to know why this ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.