Problem solve Get help with specific problems with your technologies, process and projects.

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

Dig Deeper on Microsoft SQL Server Installation