Sub-query implementation. First draft