refactored generate_join_condition_sql