Implementing the query Function