move the collection sql query into its own function