working on converting tree to sql query