Fixed the ordering issue
Added sub-query support