/*------------------------------------------------------------*/ /* Assign a row number to every row - order by lname */ /*------------------------------------------------------------*/ SELECT ROW_NUMBER() OVER(order by lname) as row_num, s.* FROM student s; /*------------------------------------------------*/ /* Assign row numbers after aggregation/grouping */ /*------------------------------------------------*/ SELECT ROW_NUMBER() OVER(order by 2 desc) as row_num, vendor, sum(amount) FROM payment GROUP BY vendor; /*------------------------------------------------*/ /* Assign row numbers after aggregation/grouping */ /* Another way */ /*------------------------------------------------*/ SELECT ROW_NUMBER() OVER(order by tot_amt desc) as row_num, subq.* FROM ( SELECT vendor, sum(amount) as tot_amt FROM payment GROUP by vendor ) subq;