Coded the function to make calls to the database encapsulating and minimizing I/O