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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:15:53+00:00 2026-06-17T10:15:53+00:00

The following two SQL queries return the same results: SELECT * FROM Table1 WHERE

  • 0

The following two SQL queries return the same results:

    SELECT * FROM Table1
    WHERE Table1.Value1 = 'something' OR Table1.Value2 IN (SELECT Value2 FROM Table2)

    SELECT * FROM Table1
    LEFT JOIN Table2 
    ON Table1.Value2 = Table2.Value2
    WHERE (Table1.Value1 = 'something' OR Table2.Value2 IS NOT NULL)

Similarly, these two queries return the same results:

    SELECT * FROM Table1
    WHERE Table1.Value1 = 'something' AND Table1.Value2 NOT IN (SELECT Value2 FROM Table2)

    SELECT * FROM Table1
    LEFT JOIN Table2
    ON Table1.Value2 = Table2.Value2
    WHERE Table1.Value1 = 'something' AND Table2.Value2 IS NULL

Personally, I find it easier to read the options that use “IN” or “NOT IN” (especially since my real query already has a pile of joins in it). However, if the number of values in Table2 grows large (currently it only returns three results), will this query become inefficient? Or will the query optimizer figure it out and turn it into a join behind the scenes? I’m using SQL Server 2012.

  • 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-06-17T10:15:54+00:00Added an answer on June 17, 2026 at 10:15 am

    The first would be better as:

    SELECT <cols> 
      FROM dbo.Table1
      WHERE Value1 = 'something' 
      OR EXISTS (SELECT 1 FROM dbo.Table2 WHERE Value2 = Table1.Value2);
    

    Though your performance problem – assuming Value2 is indexed in both locations and you are really only going to select the columns you need instead of forcing a scan or a lookup using * – is going to be the OR. You might consider this alternative, if Value1 is properly indexed, at least to test the difference in performance (you’ll want to look at the plans, not just measure time, while you have just three rows):

    SELECT <cols>
      FROM dbo.Table1 
      WHERE Value1 = 'something'
    UNION ALL
    SELECT <cols>
      FROM dbo.Table1
      WHERE Value1 <> 'something'
      AND EXISTS (SELECT 1 FROM dbo.Table2 WHERE Value2 = Table1.Value2);
    

    For the NOT IN query, this will be both more reliable and at least as efficient as the two options you offered:

    SELECT <cols>
      FROM dbo.Table1
      WHERE Value1 = 'something' 
      AND NOT EXISTS (SELECT 1 FROM dbo.Table2 WHERE Value2 = Table1.Value2);
    

    Indexing is going to be key here, but it is important to understand NOT IN and LEFT OUTER JOIN can throw you in a hole. See the following article:

    http://www.sqlperformance.com/2012/12/t-sql-queries/left-anti-semi-join

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

Sidebar

Related Questions

I have two sql queries as following SELECT rc.stateId,rs.stateName FROM (SELECT DISTINCT cityid FROM
The following two queries each give the same result: SELECT column FROM table GROUP
I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
I'm wondering why these two following conditions in SQL return different results First Condition
I have two similar queries theoretically returning the same results: var requestNotWorking = SessionManagement.Db.Linq<Item>(false).Where(i
I have two SQL queries producing different results when I would expect them to
I want to do the following two SQL Queries in Microsoft SQL SERVER UPDATE
I have two SQL queries: A. SELECT (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) + upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v))
I have two very similar SQL queries that return a count of minutes spent
I have the following two SQL statements First one: IF(@User_Id IS NULL) BEGIN SELECT

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.