Implementation of query