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 ('10', '24', '13', '20', '11', '28', '25') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('10', '24', '13', '20', '11', '28', '25') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 10 | 9068 | 6.78273e+07 11 | 9020 | 6.79646e+07 13 | 9027 | 6.76025e+07 20 | 9252 | 6.94852e+07 24 | 8945 | 6.70796e+07 25 | 9117 | 6.83908e+07 28 | 9034 | 6.7695e+07 (7 rows) COMMIT; COMMIT