I am using SQL Server 2008 R2
I just want to test if something exists in a table
IF EXISTS (SELECT * FROM ta WHERE ca = 'abc') PRINT 'YES'
IF EXISTS (SELECT ca FROM ta WHERE ca = 'abc') PRINT 'YES'
IF EXISTS (SELECT 1 FROM ta WHERE ca = 'abc') PRINT 'YES'
IF EXISTS (SELECT (1) FROM ta WHERE ca = 'abc') PRINT 'YES'
IF EXISTS (SELECT TOP 1 1 FROM ta WHERE ca = 'abc') PRINT 'YES'
Do they have any differences in result/side effect/performance (no matter how tiny)?
Thank you
Absolutely no difference – the
IF EXISTS(...)will only check for existence of rows based on theWHEREclause in your statement.Everything else in the statement is irrelevant – doesn’t make any difference whether you use
SELECT *orSELECT 1or orSELECT TOP 1 *. Even usingSELECT * ....does NOT select all columns from the table – it again just checks for existence of the data based on theWHEREclause.All five queries have exactly the same execution plan