Ask the Expert

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.

    Requires Free Membership to View

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,, tableb.location from tablea left outer join tableb on tablea.code = tableb.code


For More Information

This was first published in February 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: