Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 newbie: "invert" query results problem

Author  Topic 

matej
Starting Member

2 Posts

Posted - 2004-09-03 : 10:54:19
Hello,

I have two tables (I'll use some sample data to show my problem):

Table Users, with fields UserId and UserName
UserId ... UserName
1 ... John
2 ... Jim
3 ... Tom
4 ... Tim

Table ProjectsUsers, with fields UserId and ProjectId
UserId ... ProjectId
1 ... 11
1 ... 12
2 ... 10
2 ... 12
3 ... 10
3 ... 12
4 ... 10

So, at one point I would like to see who is working on project with ProjectId 10. I write:

SELECT * FROM Users, ProjectsUsers WHERE Users.UserId = ProjectsUsers.UserId AND ProjectsUsers.ProjectId=10

That works ok. I get:
UserId ... UserName
2 ... Jim
3 ... Tom
4 ... Tim

But now for the problem: How do I get everybody who is in table Users, but do not work on project with ProjectId 10?
The expected result would be:
UserId ... UserName
1 ... John

I realize it's probably dead simple, but I'm having trouble with it so please help. Thanks :)

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-03 : 10:59:02
SELECT *
FROM Users as A
WHERE not exists(Select 1 From ProjectUsers Where UserId = A.UserId AND ProjectId=10)



Corey
Go to Top of Page

matej
Starting Member

2 Posts

Posted - 2004-09-03 : 12:20:22
Thanks!
Go to Top of Page

lazerath
Constraint Violating Yak Guru

343 Posts

Posted - 2004-09-03 : 15:19:55
Corey,

Wouldn't this statement perform better?

[CODE]
SELECT *
FROM dbo.Users A
LEFT JOIN dbo.ProjectUsers PU
ON a.UserID = PU.UserID
AND PU.ProjectID = 10
WHERE PU.UserID IS NULL
[/CODE]
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-03 : 16:38:57
I'm not sure. Easiest thing to do would be to check the execution plans for both.

Although, in your suggestion, the query is trying to return all of the columns, not just the Users columns (very minor difference). It seems like any difference would depend on the comparison of exists to a join.

I think in some cases exist can be better as you may not need to process through multiple matches, you only need to find the first one. I don't know that it would make a difference here.

matej - what can you tell us? is the second one faster?


Corey
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-09-03 : 17:01:50
Hey what's with the "SELECT 1" in the NOT EXISTS clause Corey ?
Isn't "SELECT *" the ANSI way ?

"SELECT *" in the from clause is bad practice matej,
but maybe it was just to save typing in the post...

rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-03 : 17:04:51
"select 1" b/c there is no need to return column info. We just want to know about existence... I read it here somewhere...

Corey
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-09-03 : 17:11:09
For exists and not exists, SELECT * is the fastest way apparently. It's the only time SELECT * should be used.

Tara
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-09-03 : 17:16:33
quote:
Originally posted by Seventhnight

"select 1" b/c there is no need to return column info. We just want to know about existence... I read it here somewhere...

Corey


Doesn't need to return columninfo with select *, it's just an existance test.
Maybe this is a myth that was true for some other dbms

rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-04 : 00:40:36
Oh well... i suppose you can not only learn here, but also unlearn...

Corey
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2004-09-04 : 09:46:11
I read somewhere that the reason SELECT * is quicker for EXISTS is that SQL Server can determine the best index to use for subquery, and that certainly helps when you have clustered index on the table.

OS
Go to Top of Page
   

- Advertisement -