[sql] How to write subquery inside the OUTER JOIN Statement