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 ('13', '10', '24', '21', '17', '32', '11') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('13', '10', '24', '21', '17', '32', '11') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 10 | 9072 | 6.78473e+07 11 | 9023 | 6.79796e+07 13 | 9029 | 6.76125e+07 17 | 9084 | 6.81695e+07 21 | 9092 | 6.83046e+07 24 | 8952 | 6.71146e+07 32 | 9115 | 6.8485e+07 (7 rows) COMMIT; COMMIT