SELECT * FROM MyTable WHERE (@name is NULL OR Name LIKE '%'+@name+'%') AND (@class is null OR Class LIKE '%'+@class+'%')
и тдoption(recompile)
SELECT u.name, u.photo
FROM black_list AS b
JOIN users AS u ON b.ban_id=u.id
WHERE b.user_id IN (3)