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 ('33', '27', '29', '21', '20', '10', '30') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('33', '27', '29', '21', '20', '10', '30') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 10 | 9062 | 6.77973e+07 20 | 9249 | 6.94703e+07 21 | 9085 | 6.82697e+07 27 | 9015 | 6.7646e+07 29 | 9201 | 6.90254e+07 30 | 9344 | 7.0124e+07 33 | 9182 | 6.88951e+07 (7 rows) COMMIT; COMMIT