refactoring piece of code where the query is executed and the value is returned