EXPLAIN select ps_partkey, sum(ps_supplycost * ps_availqty) as value from partsupp, supplier, nation where ps_suppkey = s_suppkey and s_nationkey = n_nationkey and n_name = 'EGYPT' group by ps_partkey having sum(ps_supplycost * ps_availqty) > ( select sum(ps_supplycost * ps_availqty) * 0.0000100000 from partsupp, supplier, nation where ps_suppkey = s_suppkey and s_nationkey = n_nationkey and n_name = 'EGYPT' ) order by value desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------- Sort (cost=170992.23..171788.33 rows=318440 width=12) Sort Key: (sum((partsupp.ps_supplycost * (partsupp.ps_availqty)::double precision))) InitPlan 1 (returns $2) -> Aggregate (cost=46966.64..46966.65 rows=1 width=8) -> Nested Loop (cost=79.73..44578.34 rows=318440 width=8) -> Nested Loop (cost=79.29..2392.61 rows=4000 width=4) -> Seq Scan on nation nation_1 (cost=0.00..1.31 rows=1 width=4) Filter: (n_name = 'EGYPT'::bpchar) -> Bitmap Heap Scan on supplier supplier_1 (cost=79.29..2351.30 rows=4000 width=8) Recheck Cond: (s_nationkey = nation_1.n_nationkey) -> Bitmap Index Scan on i_s_nationkey (cost=0.00..78.29 rows=4000 width=0) Index Cond: (s_nationkey = nation_1.n_nationkey) -> Index Scan using i_ps_suppkey on partsupp partsupp_1 (cost=0.43..9.75 rows=80 width=12) Index Cond: (ps_suppkey = supplier_1.s_suppkey) -> GroupAggregate (cost=79127.31..89476.61 rows=318440 width=12) Group Key: partsupp.ps_partkey Filter: (sum((partsupp.ps_supplycost * (partsupp.ps_availqty)::double precision)) > $2) -> Sort (cost=79127.31..79923.41 rows=318440 width=12) Sort Key: partsupp.ps_partkey -> Nested Loop (cost=79.73..44578.34 rows=318440 width=12) -> Nested Loop (cost=79.29..2392.61 rows=4000 width=4) -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) Filter: (n_name = 'EGYPT'::bpchar) -> Bitmap Heap Scan on supplier (cost=79.29..2351.30 rows=4000 width=8) Recheck Cond: (s_nationkey = nation.n_nationkey) -> Bitmap Index Scan on i_s_nationkey (cost=0.00..78.29 rows=4000 width=0) Index Cond: (s_nationkey = nation.n_nationkey) -> Index Scan using i_ps_suppkey on partsupp (cost=0.43..9.75 rows=80 width=16) Index Cond: (ps_suppkey = supplier.s_suppkey) (29 rows)