Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1022753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:30:44+00:00 2026-05-16T11:30:44+00:00

I’m joining two tables on a composite key, and I’m wondering if it matters

  • 0

I’m joining two tables on a composite key, and I’m wondering if it matters where I compare the corresponding columns when I do the join.

Say I have a table, TableA, with columns ColAFoo, ColAFoo2, and ColABar. TableA has a composite primary key comprising ColAFoo and ColAFoo2 (PK_TableA).

I also have TableB, with ColBFoo, ColBFoo2, and ColBOther. TableB’s columns ColBFoo and ColBFoo2 comprise a foreign key to TableA’s primary key (FK_TableA_TableB).

I need to join the two tables on the key. Is there a difference between the following three (extraordinarily contrived) statements in terms of performance?

SELECT *
  FROM TableA a
  JOIN TableB b
    ON  a.ColAFoo = b.ColBFoo
        AND a.ColAFoo2 = b.ColBFoo2

SELECT *
  FROM TableA a
  JOIN TableB b
    ON  a.ColAFoo = b.ColBFoo
  WHERE a.ColAFoo2 = b.ColBFoo2

-- this one is a little /too/ contrived, apparently (see comments)
SELECT *
  FROM TableA a
  JOIN TableB b
  WHERE a.ColAFoo = b.ColBFoo
        AND a.ColAFoo2 = b.ColBFoo2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:30:44+00:00Added an answer on May 16, 2026 at 11:30 am

    For an inner join the following are equivelent in results, and will probably produce the same query plan:

    SELECT *
      FROM TableA a
      JOIN TableB b
        ON  a.ColAFoo = b.ColBFoo
            AND a.ColAFoo2 = b.ColBFoo2
    
    SELECT *
      FROM TableA a
      JOIN TableB b
        ON  a.ColAFoo = b.ColBFoo
      WHERE a.ColAFoo2 = b.ColBFoo2
    
    -- SQL89 inner join:
    SELECT *
      FROM TableA a, TableB b
     WHERE a.ColAFoo = b.ColBFoo
           AND a.ColAFoo2 = b.ColBFoo2
    

    However Putting the join criteria in the ON clause will communicate to other programmers, “Hey! This is the criteria to relate the tables together.” Versus stuff in the where clause that is, “criteria to limit the results, after the joins are done.”

    Also, the placement of the criteria makes a big difference in results when using an outer join, so it is a good habit to get into to put the join criteria in the on in all cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.