create table user (
id int primary key auto_increment,
name varchar(128)
);
insert into user (name) values ('Charle'), ('Clara');
create table account (
id int primary key auto_increment,
user_id int references user(id),
currency enum ('EUR', 'USD', 'GBP')
);
insert into account(user_id, currency) values (1, 'EUR'),(2, 'EUR'),(1, 'EUR');
create table operation (
id int primary key auto_increment,
account_id int references account(id),
type Enum ('income', 'expense'),
amount INT
);
insert into operation (account_id, type, amount) values
(1, 'income', 100), (1, 'income', 100), (2, 'income', 300), (3, 'income', 1000);
select u.name, sum(amount)
from account a
join user u on u.id = a.user_id
join operation o on o.account_id = a.id
where a.currency = 'EUR'
group by u.id, u.name
having sum(amount) > (
select avg(amount)
from account a
join operation o on o.account_id = a.id
where a.currency = 'EUR'
)