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 710013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:34:03+00:00 2026-05-14T04:34:03+00:00

Whats the difference between SELECT DISTINCT field1 FROM table1 cd JOIN table2 ON cd.Company

  • 0

Whats the difference between

SELECT DISTINCT field1 
  FROM table1 cd  
  JOIN table2 
    ON     cd.Company = table2.Name 
       and table2.Id IN (2728) 

and

SELECT DISTINCT field1 
  FROM table1 cd  
  JOIN table2 
    ON cd.Company = table2.Name 
 where table2.Id IN (2728) 

both return the same result and both have the same explain output

  • 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-14T04:34:03+00:00Added an answer on May 14, 2026 at 4:34 am

    Firstly there is a semantic difference. When you have a join, you are saying that the relationship between the two tables is defined by that condition. So in your first example you are saying that the tables are related by cd.Company = table2.Name AND table2.Id IN (2728). When you use the WHERE clause, you are saying that the relationship is defined by cd.Company = table2.Name and that you only want the rows where the condition table2.Id IN (2728) applies. Even though these give the same answer, it means very different things to a programmer reading your code.

    In this case, the WHERE clause is almost certainly what you mean so you should use it.

    Secondly there is actually difference in the result in the case that you use a LEFT JOIN instead of an INNER JOIN. If you include the second condition as part of the join, you will still get a result row if the condition fails – you will get values from the left table and nulls for the right table. If you include the condition as part of the WHERE clause and that condition fails, you won’t get the row at all.

    Here is an example to demonstrate this.

    Query 1 (WHERE):

    SELECT DISTINCT field1
      FROM table1 cd
      LEFT JOIN table2
        ON cd.Company = table2.Name
     WHERE table2.Id IN (2728);
    

    Result:

    field1
    200
    

    Query 2 (AND):

    SELECT DISTINCT field1
      FROM table1 cd
      LEFT JOIN table2
        ON cd.Company = table2.Name
       AND table2.Id IN (2728);
    

    Result:

    field1
    100
    200
    

    Test data used:

    CREATE TABLE table1 (Company NVARCHAR(100) NOT NULL, Field1 INT NOT NULL);
    INSERT INTO table1 (Company, Field1) VALUES
    ('FooSoft', 100),
    ('BarSoft', 200);
    
    CREATE TABLE table2 (Id INT NOT NULL, Name NVARCHAR(100) NOT NULL);
    INSERT INTO table2 (Id, Name) VALUES
    (2727, 'FooSoft'),
    (2728, 'BarSoft');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

whats the difference between.. select a.value1 from apple a, banana b, oranges o where
What's the difference between the following two SQLs: select ... from table1 t1 left
Whats the difference between using a LEFT OUTER JOIN , rather than a sub-query
What is the difference between .Select() and .Where() in Entity Framework? Eg return ContextSet().Select(x=>
What is the difference between: sqlCmd = SELECT * FROM dbo.Products WITH (NOLOCK); List<Product>
What is difference between: SELECT * FROM news WHERE cat_id IN (1, 2, 6,
What is the difference between select empName as EmployeeName from employees versus select EmployeeName
What is the difference between SELECT data from table directly or from view? And
What´s the difference between: $sql = select * from {$table}; and this: $sql =
What is the difference between using SELECT ... INTO MyTable FROM... and INSERT INTO

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.