Single sql query working