add ''' and instances.deleted=0''' to the end of this query