Fixed database query