Totals per group, sorted
Any aggregate works per group: SUM, AVG, MIN, MAX as well as COUNT. And you can sort the
groups by the result, which is how you answer "which is biggest?":
SELECT status, SUM(net_amount) AS net_total
FROM invoices
GROUP BY status
ORDER BY net_total DESCORDER BY can use the alias you gave the aggregate (net_total). You could also write
ORDER BY SUM(net_amount) DESC, which works in every database.
Other databases: sorting by a column alias works in DuckDB, PostgreSQL, MySQL, SQL Server and BigQuery. Using an alias in
WHEREorGROUP BYis less portable, so repeat the expression there.