sqlite3 data.db 'SELECT dept, ROUND(AVG(salary),2) AS avg_salary FROM employees GROUP BY dept ORDER BY avg_salary DESC;'

Category: Data Science & Analysis

sqlite3 data.db 'SELECT dept, ROUND(AVG(salary),2) AS avg_salary FROM employees GROUP BY dept ORDER BY avg_salary DESC;'

Rank departments by rounded average salary

ROUND(AVG(salary),2) rounds each department's average to 2 decimal places for tidy output, aliased as avg_salary with AS. The ORDER BY then sorts the grouped result by that alias, putting the highest-paying department first. Aliases can be referenced in ORDER BY but not in WHERE. This is the canonical grouped ranking query for SQLite.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.