Joining tables without a primary key

Can you solve this in MS SQL-Server with the help of a SQL query. The scenario is as follows:


 Table A ======= Code Name CE Java CE Oracle CE Microsoft AE Sun AE Digital AE HP AR Compaq Table B ======= Code Location CE US CE UK AE Japan

Now with the help of SQL query, please produce the output:

 ----------------------------------------------------- Code Name Location ----------------------------------------------------- CE Java US CE Oracle UK CE Microsoft NULL AE Sun Japan AE Digital NULL AE HP NULL AR Compaq NULL
Common field is Code between both the tables. But none of the tables has a Primary Key.

A primary key is not required. A foreign key is not required either. You can construct a query joining two tables on any column you wish as long as the datatypes either match or are converted to match. No relationship needs to explicitly exist.

To do this you use an outer join:

 select tablea.code, tablea.name, tableb.location from tablea left outer join tableb on tablea.code = tableb.code


For More Information

This was first published in February 2002

Dig deeper on Microsoft SQL Server Installation



Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.



Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: