• Hi John,

    It´s allways a good rule of thumb to use TOP 1 together with EXISTS, because it prevents the database engine from doing unneccesary work.

    I you write:

    select *

    from OneTable

    where EXISTS (select * from SecondTable where OneCol = AnotherCol)

    The database engine will first get ALL rows in the SecondTable, then it will check if the result has any rows.

    If you instead specify TOP 1:

    select *

    from OneTable

    where EXISTS (select TOP 1 * from SecondTable where OneCol = AnotherCol)

    The database engine will only fetch ONE row from the SecondTable, then it will check if the result has any rows.

    The same thing is applicable for TSQL statements like:

    IF ((select count(*) from myTable) > 0)

    ...

    If you change this to:

    IF (EXISTS (select top 1 * from myTable))

    The database engine doesn´t have to count the rows, when you aren´t even interrested in how many there are...

    If the databases are small the performance difference might not be so huge, but it´s allways a good habbit. 🙂

    /Markus