improve code for both scalar subquery and table subquery.