BEGIN; BEGIN select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('16', '13', '14', '21', '19', '32', '18') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('16', '13', '14', '21', '19', '32', '18') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 13 | 90769 | 6.79635e+08 14 | 91472 | 6.85891e+08 16 | 90639 | 6.79784e+08 18 | 91313 | 6.84694e+08 19 | 90902 | 6.81896e+08 21 | 90919 | 6.8172e+08 32 | 90690 | 6.80569e+08 (7 rows) COMMIT; COMMIT