witten script to generate sql query