database query optimization and pagination exercises