Implemented some selection and filtering logic to select data from the database.