Home All Groups Group Topic Archive Search About

NOT EXISTS? Finding records without a correlating record in another table

Author
2 Mar 2006 9:34 PM
KBuser
I'm working with (essentially) two tables here.
Magazine.(MagazineID, PK) and Ratedetail.(ratedetailID, PK)(magazineID,
FK)

I need a statement that returns all the magazineIDs that do not have a
ratedetailid. I'm assuming not exists is what i'm looking to use, but
am not having any luck with it.

I don't think I need to supply any dummy data or go into more details
with the schema... but I will if requested.... Thanks in advance, Krb

Author
2 Mar 2006 9:52 PM
Aaron Bertrand [SQL Server MVP]
> I'm working with (essentially) two tables here.
> Magazine.(MagazineID, PK) and Ratedetail.(ratedetailID, PK)(magazineID,
> FK)
>
> I need a statement that returns all the magazineIDs that do not have a
> ratedetailid. I'm assuming not exists is what i'm looking to use, but
> am not having any luck with it.

SELECT m.* FROM
    Magazine m
    LEFT OUTER JOIN RateDetail r
    ON m.MagazineID = r.MagazineID
    WHERE r.MagazineID IS NULL;

or

SELECT * FROM Magazine m
    WHERE NOT EXISTS
    (SELECT MagazineID FROM RateDetail r
        WHERE r.MagazineID = m.MagazineID);

> I don't think I need to supply any dummy data or go into more details
> with the schema... but I will if requested...

This should be automatic, imho, but I realize that's a lofty goal.  I don't
think most people understand how much easier it makes it to run your
existing schema, test our queries against it, and make sure we provide you
with the right answer, instead of making all kinds of guesses about what you
want returned, etc.  We don't ask for DDL and sample data just to mak you
work harder to get an answer, we actually do it to save everybody time and
allow us to help more people in a day.
Author
2 Mar 2006 9:59 PM
KBuser
Aaron Bertrand [SQL Server MVP] wrote:
> This should be automatic, imho, but I realize that's a lofty goal.  I don't
> think most people understand how much easier it makes it to run your
> existing schema, test our queries against it, and make sure we provide you
> with the right answer, instead of making all kinds of guesses about what you
> want returned, etc.  We don't ask for DDL and sample data just to mak you
> work harder to get an answer, we actually do it to save everybody time and
> allow us to help more people in a day.

I appreciate your time and effort, and do realize that sample
data/schema info is quite important, but I thought my question was
basic enough to get query I was looking for. I believe I will be able
to take your (2nd) suggestion and compile my own query to achieve my
desired output. I also don't want people to do "all" of my work for me,
but rather point me in the right direction so that I still have to put
some effort into learning how things actually work. Thanks again, Krb.
Author
2 Mar 2006 10:06 PM
Aaron Bertrand [SQL Server MVP]
> I appreciate your time and effort, and do realize that sample
> data/schema info is quite important, but I thought my question was
> basic enough to get query I was looking for.

Yes, it was, I was speaking more in the general sense.  That doesn't mean
including it would make our lives more difficult.  It just means that if you
don't include it, you may get untested and/or undesired solutions.

A

AddThis Social Bookmark Button