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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:36:21+00:00 2026-05-31T01:36:21+00:00

I have a sql that is not very complex but sufficiently confusion that I

  • 0

I have a sql that is not very complex but sufficiently confusion that I question rather I have an equivalent or by coincident that the count are the same.

SQL1:

SELECT a, b
FROM table1
WHERE NOT EXISTS(
  SELECT a, c
  FROM TABLE2
  WHERE table2.a != table1.a)

SQL2

SELECT table1.a, table1.b
FROM table1
LEFT JOIN table2 ON table2.a = table1.a
WHERE table2.a IS NULL

The count on the two are identical, but not sure if this is by chance, and I want to make sure the conversion do not change the original functionality.

  • 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-31T01:36:22+00:00Added an answer on May 31, 2026 at 1:36 am

    The first query, as you have it, returns all rows of TABLE1 where a matches all values of a in TABLE2. Therefore, it will return zero rows, unless there’s a single not-null value for a in TABLE2, and that value exists in TABLE1. In that case, it will return as many rows as there are in TABLE1 with that value of a.

    The second query is completely different. It will simply returns all rows of TABLE1 where a does not exist in TABLE2.

    So it’s “matches all” (query 1) vs. “does not match any” (query 2). The fact that you are getting the same number of rows is pure coincidence.

    Your queries would be equivalent if you changed != for = in the first one, like this:

    SELECT a, b
    FROM table1
    WHERE NOT EXISTS(
      SELECT a, c
      FROM TABLE2
      WHERE table2.a = table1.a)
    

    That gets you values of a in table1 that doesn’t exist in table2. This is EXACTLY the same as:

    SELECT table1.a, b
    FROM table1
    LEFT JOIN table2 ON table2.a = table1.a
    WHERE table2.a IS NULL
    

    As you have it though, they are NOT equivalent. You must change != for = in the first one to make them so.

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

Sidebar

Related Questions

I have a very complex Linq to SQL query that returns a result set
I have a SQL Server 2000 database that will not display the column list
I have a sql query that runs super fast, around one second, when not
I have text stored in SQL as HTML. I'm not guaranteed that this data
I have read that while plug-ins are not supported for SQL Server Management Studio,
SQL Server Express does not have the Tasks -> Import Data option that other
I have multiple databases that I connect to using SQL*Plus in Windows (not the
I have an sql query that counts the number of results for a complex
I have made a simple, but relatively computationally complex, UDF that queries a rarely
Disclaimer: I understand the question is very basic, but I could not find the

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.