working on sql logic