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

  • Home
  • SEARCH
  • 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 201549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:12:32+00:00 2026-05-11T17:12:32+00:00

I have a case where I wanna choose any database entry that have an

  • 0

I have a case where I wanna choose any database entry that have an invalid Country, Region, or Area ID, by invalid, I mean an ID for a country or region or area that no longer exists in my tables, I have four tables: Properties, Countries, Regions, Areas.
I was thinking to do it like this:

SELECT * FROM Properties WHERE 
Country_ID NOT IN 
(
SELECT CountryID FROM Countries
)
OR
RegionID NOT IN
(
SELECT RegionID FROM Regions
)
OR
AreaID NOT IN
(
SELECT AreaID FROM Areas
)

Now, is my query right? and what do you suggest that i can do and achieve the same result with better performance?!

  • 1 1 Answer
  • 2 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-11T17:12:32+00:00Added an answer on May 11, 2026 at 5:12 pm

    Your query in fact is optimal.

    LEFT JOIN‘s proposed by others are worse, as they select ALL values and then filter them out.

    Most probably your subquery will be optimized to this:

    SELECT  *
    FROM    Properties p
    WHERE   NOT EXISTS
            (
            SELECT  1
            FROM    Countries i
            WHERE   i.CountryID = p.CountryID
            )
            OR
            NOT EXISTS
            (
            SELECT  1
            FROM    Regions i
            WHERE   i.RegionID = p.RegionID
            )
            OR
            NOT EXISTS
            (
            SELECT  1
            FROM    Areas i
            WHERE   i.AreaID = p.AreaID
            )
    

    , which you should use.

    This query selects at most 1 row from each table, and jumps to the next iteration right as it finds this row (i. e. if it does not find a Country for a given Property, it will not even bother checking for a Region).

    Again, SQL Server is smart enough to build the same plan for this query and your original one.

    Update:

    Tested on 512K rows in each table.

    All corresponding ID‘s in dimension tables are CLUSTERED PRIMARY KEY‘s, all measure fields in Properties are indexed.

    For each row in Property, PropertyID = CountryID = RegionID = AreaID, no actual missing rows (worst case in terms of execution time).

    NOT EXISTS    00:11 (11 seconds)
    LEFT JOIN     01:08 (68 seconds)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following case , and i wanna to ask what's the best
I have a case that keeps coming up where I'm using a ListView or
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have a case sensitive SERVER (SQL_Latin1_General_CP1_CS_AS) but the Database is Case insensitive (SQL_Latin1_General_CP1_CI_AS).
I have developed an app named Rockfish Motorsports case u wanna dl it :)
sometimes i face the following case in my database design,, i wanna to know
Well. Hi everybody again. I have a trouble with some script that I wanna
Q: I have the following case : Div contains a link , i wanna
I have a case where a 3rd party ad is bleeding through my modal
I have a case where if a SharePoint site owner decides to break permissions

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.