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 ('25', '22', '30', '26', '12', '14', '23') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('25', '22', '30', '26', '12', '14', '23') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 12 | 9031 | 6.77686e+07 14 | 8996 | 6.77164e+07 22 | 8987 | 6.72569e+07 23 | 8974 | 6.75579e+07 25 | 9102 | 6.83158e+07 26 | 9006 | 6.76699e+07 30 | 9334 | 7.00739e+07 (7 rows) COMMIT; COMMIT