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 ('19', '29', '30', '21', '25', '26', '20') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('19', '29', '30', '21', '25', '26', '20') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 19 | 9077 | 6.81724e+07 20 | 9237 | 6.94102e+07 21 | 9068 | 6.81847e+07 25 | 9104 | 6.83258e+07 26 | 9009 | 6.76849e+07 29 | 9188 | 6.89604e+07 30 | 9335 | 7.00789e+07 (7 rows) COMMIT; COMMIT