SELECT
ntile,
CAST(avg(length) AS INTEGER) AS avgAmount,
CAST(max(length) AS INTEGER) AS maxAmount,
CAST(min(length) AS INTEGER) AS minAmount
FROM (SELECT length, ntile(6) OVER (ORDER BY length) AS ntile FROM countries) x
GROUP BY ntile
ORDER BY ntile;
ntile | avgamount | maxamount | minamount
-------+-----------+-----------+-----------
1 | 2147395 | 3831430 | 76366
2 | 5000860 | 6155808 | 3837343
3 | 7570729 | 9008463 | 6157514
4 | 10563760 | 12137060 | 9008463
5 | 14479508 | 18657806 | 12153800
6 | 26754083 | 84192008 | 18773988
(6 rows)