Is there a way to do this with SQL? or any other method?