CON-47: Fix params to select and fix SQL by using subquery