BEGIN; BEGIN EXPLAIN select c_count, count(*) as custdist from ( select c_custkey, count(o_orderkey) from customer left outer join orders on c_custkey = o_custkey and o_comment not like '%special%packages%' group by c_custkey ) as c_orders (c_custkey, c_count) group by c_count order by custdist desc, c_count desc; QUERY PLAN ------------------------------------------------------------------------------------------------ Sort (cost=918945.74..918946.24 rows=200 width=8) Sort Key: (count(*)), (count(orders.o_orderkey)) -> HashAggregate (cost=918936.10..918938.10 rows=200 width=8) -> HashAggregate (cost=881436.10..896436.10 rows=1500000 width=8) -> Hash Right Join (cost=69028.00..807108.28 rows=14865564 width=8) Hash Cond: (orders.o_custkey = customer.c_custkey) -> Seq Scan on orders (cost=0.00..440769.00 rows=14865564 width=8) Filter: ((o_comment)::text !~~ '%special%packages%'::text) -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (10 rows) COMMIT; COMMIT