fixed database query