Comparison does not work when the array element does not exist, leading to an incorrect sql query generated