Implemented the query Function