I am checking if there are any rows in a table with some conditions.  If there are more than 1 row, it should filter again with some more conditions.DECLARE @Matches AS intDECLARE @Result AS intSET @Matches =  (    Select COUNT(*)        FROM Houses             INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey        WHERE Houses.ID IS NOT NULL AND Houses.ID = @DoorKey AND UserAccess.UserKey = @UserKey)IF(@Matches < 1) SET @Result = 0ELSE IF(@Matches > 0)    BEGIN        SET @Matches =          (        Select COUNT(*)        FROM Houses             INNER JOIN Houses_Access ON Houses.ID = Houses_Access.DoorKey            FULL OUTER Join UserAccess ON  Houses_Access.GroupKey  = UserAccess.GroupKey        WHERE Houses.ID IS NOT NULL AND Houses.ID = @DoorKey AND UserAccess.UserKey = @UserKey            AND Houses_Access.GroupKey != Houses.OwnerGroupKey         )        IF(@Matches < 1)            SET @Result = 0        ELSE            SET @Result = 1    ENDRETURN(@Result)END
Reusing select statement
sql;sql server