added sql query avg algo