Write Sql Query In Python Iteration