IntermediateGROUP BY & Aggregation
Histogram: salary distribution with width_bucket
The query
SQL
SELECT
WIDTH_BUCKET(salary, 20000, 150000, 13) AS bucket,
(20000 + (WIDTH_BUCKET(salary, 20000, 150000, 13) - 1) * 10000)::TEXT || 'k-'
|| (20000 + WIDTH_BUCKET(salary, 20000, 150000, 13) * 10000)::TEXT || 'k' AS range,
COUNT(*) AS employees,
REPEAT('█', COUNT(*)) AS bar
FROM employees
WHERE salary IS NOT NULL
GROUP BY bucket
ORDER BY bucket;Note
“WIDTH_BUCKET distributes values into equal-width buckets. REPEAT draws ASCII bar chart. Classic histogram query.