EXPLAIN select c_name, c_custkey, o_orderkey, o_orderdate, o_totalprice, sum(l_quantity) from customer, orders, lineitem where o_orderkey in ( select l_orderkey from lineitem group by l_orderkey having sum(l_quantity) > 315 ) and c_custkey = o_custkey and o_orderkey = l_orderkey group by c_name, c_custkey, o_orderkey, o_orderdate, o_totalprice order by o_totalprice desc, o_orderdate LIMIT 100; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=13449486.36..13449489.11 rows=100 width=39) -> GroupAggregate (cost=13449486.36..14274291.88 rows=29992928 width=39) Group Key: orders.o_totalprice, orders.o_orderdate, customer.c_name, customer.c_custkey, orders.o_orderkey -> Sort (cost=13449486.36..13524468.68 rows=29992928 width=39) Sort Key: orders.o_totalprice, orders.o_orderdate, customer.c_name, customer.c_custkey, orders.o_orderkey -> Hash Join (cost=4097014.04..7264286.78 rows=29992928 width=39) Hash Cond: (lineitem.l_orderkey = orders.o_orderkey) -> Seq Scan on lineitem (cost=0.00..1665268.56 rows=59985856 width=8) -> Hash (cost=3944670.94..3944670.94 rows=7499928 width=39) -> Hash Join (cost=77827.63..3944670.94 rows=7499928 width=39) Hash Cond: (orders.o_custkey = customer.c_custkey) -> Merge Semi Join (cost=1.00..3620165.75 rows=7499928 width=20) Merge Cond: (orders.o_orderkey = lineitem_1.l_orderkey) -> Index Scan using orders_pkey on orders (cost=0.43..642799.26 rows=14999855 width=16) -> Materialize (cost=0.56..2934261.46 rows=448431 width=4) -> GroupAggregate (cost=0.56..2928656.07 rows=448431 width=8) Group Key: lineitem_1.l_orderkey Filter: (sum(lineitem_1.l_quantity) > 315::double precision) -> Index Scan using i_l_orderkey on lineitem lineitem_1 (cost=0.56..2623121.40 rows=59985856 width=8) -> Hash (cost=50285.17..50285.17 rows=1500117 width=23) -> Seq Scan on customer (cost=0.00..50285.17 rows=1500117 width=23) (21 rows)