written query functio