working on sql for custom templates