title: How can I update rows obtained by a SELECT query in PostgreSQL with Python? Python 2.7 psycopg2
body: <p>I have the following code to calculate a value in specific rows of my table:</p>
  
  <pre><code>cursor.execute("SELECT * FROM restaurants WHERE license_type_code='20' ORDER BY general_score DESC;")
  group_size = cursor.rowcount
  for record in cursor:
     index = cursor.rownumber
     percentile = 100*(index - 0.5)/group_size
     print percentile
  </code></pre>
  
  <p>What I need to do is to add the <code>percentile</code> result to the respective column <code>score_percentile</code> of each record I got with the <code>SELECT</code> query.</p>
  
  <p>I thought about an <code>UPDATE</code> query like this:</p>
  
  <pre><code> cursor.execute("UPDATE restaurants SET score_percentile="+str(percentile)+" WHERE license_type_code IN (SELECT * FROM restaurants WHERE license_type_code='20' ORDER BY general_score DESC)")
  </code></pre>
  
  <p>But I don't know if that query is correct or if there's a more efficient and less silly way to do that (I'm sure there has to be).
  Could you help me, please?</p>
  
  <p>I'm new with SQL so any help or advice is highly appreciated.
  Thanks! </p>