result curs fetchall(result (('bob' )('ann' )('kim' )tot for (namepayin resulttot +pay print('total:'tot'average:'tot len(result)total average use /to truncate or we can use more advanced tools such as comprehensions and generator expressions to calculate sumsaveragesmaximumsand the likeprint(sum(rec[ for rec in result)generator expr print(sum(rec[ for rec in resultlen(result) print(max(rec[ for rec in result) the python approach is more generalbut it doesn' buy us much until things become more complex for examplehere are few more advanced comprehensions that collect the names of people whose pay is above and below the average in the query result setavg sum(rec[ for rec in resultlen(resultprint([rec[ for rec in result if rec[ avg]['kim'print([rec[ for rec in result if rec[ avg]['ann'we may be able to do some of these kinds of tasks with more advanced sql techniques such as nested queriesbut we eventually reach complexity threshold where python' general-purpose nature makes it attractive and potentially more portable for comparisonhere is the equivalent sqlquery ("select name from people where job 'develand "pay (select avg(payfrom people where job 'devel')"curs execute(querycurs fetchall([('kim',)query ("select name from people where job 'develand "pay (select avg(payfrom people where job 'devel')"curs execute(querycurs fetchall([('ann',)this isn' the most complex sql you're likely to meetbut beyond this pointsql can become more involved moreoverunlike pythonsql is limited to database-specific tasks by design imagine query that compares column' values to data fetched off the web or from user in gui--simple with python' internet and gui supportbut well beyond the scope of special-purpose language such as sql by combining python and sqlyou get the best of both and can choose which is best suited to your goals databases and persistence