Do embedded matching within SQL query instead of in code