title: Execute SQL CTE using SQLAlchemy and Python
body: <p>I have the following code in the main program:</p>
  <pre><code>cleanDuplicatesAttendeesSQL = attendees.clean_duplicates()
  sqlUtilities.databaseWorkerExecute(cleanDuplicatesAttendeesSQL)
  </code></pre>
  <p><code>attendees.clean_duplicates()</code> sets the variable <code>cleanDuplicatesAttendeesSQL</code>:</p>
  <pre><code>def clean_duplicates():
      return &quot;&quot;&quot;
          WITH cte AS(
              SELECT [Sessions]
                  ,[Confirmation #]
                  ,[First Name]
                  ,[Last Name]
                  ,ROW_NUMBER() OVER (
                      PARTITION BY
                          [Sessions]
                          ,[Confirmation #]
                          ,[First Name]
                          ,[Last Name]
                      ORDER BY
                          [Sessions]
                          ,[Confirmation #]
                          ,[First Name]
                          ,[Last Name]
                      ) row_num
              FROM attendees
          )
          DELETE FROM cte
          WHERE row_num &gt; 1&quot;&quot;&quot;
  </code></pre>
  <p>I then pass <code>cleanDuplicatesAttendeesSQL</code> to the following function to execute against the database:</p>
  <pre><code>def databaseWorkerExecute (sql):
      engine = sqlalchemy_connect()
      try:
  
          engine.execute(sql)
          print(&quot;Execution successful.&quot;)
      except SQLAlchemyError as e:
          error = str(e.__dict__['orig'])
          print (error)
          sys.exit(&quot;Execution was not sucessful. Exiting the program...&quot;)
  </code></pre>
  <p>This function does not error, I get an &quot;Execution successful&quot; every time. However, when I look at the table in the database I still see duplicates. I successfully use this same <code>databaseWorkerExecute</code> function to do other things like updates, inserts, and deletes. How can I get the CTE to also work?</p>
  <p>Screenshot of SQL output:
  <img src="https://i.stack.imgur.com/e5TLC.png" alt="Screenshot of SQL output" /></p>