Wrap query into a function