first attempt to write sql functions