SELECT `query`.*, (`pos_from`.`created_at` - `pos_to`.`created_at`) `pos_result_date`, FROM `query`
LEFT JOIN `query_position` `pos_from` ON (`query`.`query_id` = `pos_from`.`query_id`) AND (DATE_FORMAT(`pos_from`.`created_at`, '%d-%m-%Y') = '07-06-2017')
LEFT JOIN `query_position` `pos_to` ON (`query`.`query_id` = `pos_to`.`query_id`) AND (DATE_FORMAT(`pos_to`.`created_at`, '%d-%m-%Y') = '10-06-2017')
GROUP BY `query`.`query_id`