preventing duplicated sql joins