import pyodbc conn = pyodbc.connect(driver='{SQL Server}', host='localhost,1433', database='pubs', user='id', password='pass') cursor = conn.cursor() cursor.execute(""" select type , count(*) cnt from dbo.titles group by type order by 2 desc """) rows = cursor.fetchall() for row in rows: print (row.type, row.cnt)