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', '24', '15', '16', '33', '29', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('25', '24', '15', '16', '33', '29', '17') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 15 | 9064 | 6.79772e+07 16 | 8980 | 6.72987e+07 17 | 9084 | 6.81695e+07 24 | 8952 | 6.71146e+07 25 | 9121 | 6.84108e+07 29 | 9214 | 6.90903e+07 33 | 9188 | 6.8925e+07 (7 rows) COMMIT; COMMIT