Connecting Tech Pros Worldwide Forums | Help | Site Map

Argument list in EXISTS/NOT EXISTS Selects

Newbie
 
Join Date: Jul 2007
Location: Austin, TX
Posts: 1
#1: Jul 10 '07
I've wondered what is best to use as an argument list in where clauses where I use an EXISTS or NOT EXISTS such as in

SELECT a,b,c
FROM dbo.Customer c (NOLOCK)
WHERE EXISTS (SELECT * FROM Orders o (NOLOCK)
WHERE o.customerID = c.customerID)

Is there any difference between using the wildcard * or a constant 'X' or a single field name?

Newbie
 
Join Date: Jul 2007
Posts: 28
#2: Jul 10 '07

re: Argument list in EXISTS/NOT EXISTS Selects


Quote:

Originally Posted by mbyrdtx

I've wondered what is best to use as an argument list in where clauses where I use an EXISTS or NOT EXISTS such as in

SELECT a,b,c
FROM dbo.Customer c (NOLOCK)
WHERE EXISTS (SELECT * FROM Orders o (NOLOCK)
WHERE o.customerID = c.customerID)

Is there any difference between using the wildcard * or a constant 'X' or a single field name?

From what I understand the following statement
SELECT * FROM Table1
is much slower than
SELECT Field1, Field2 From Table1

The idea being that "select *" forces sql server to perform many more actions to return the result set. You are better off typing out every field you want returned.

I do not know if a constant instead of a field name will give you better performace.
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#3: Jul 10 '07

re: Argument list in EXISTS/NOT EXISTS Selects


Quote:

Originally Posted by mbyrdtx

I've wondered what is best to use as an argument list in where clauses where I use an EXISTS or NOT EXISTS such as in

SELECT a,b,c
FROM dbo.Customer c (NOLOCK)
WHERE EXISTS (SELECT * FROM Orders o (NOLOCK)
WHERE o.customerID = c.customerID)

Is there any difference between using the wildcard * or a constant 'X' or a single field name?


try:

select a,b,c from dbo.Customer C where c.CustomerId in (select o.customerID from Orders)
Reply