updated the code using query