Implementation of basic queries in Java