Basically you want this.
The problem is that john has 2 user_ids 1 and 6
Else i would put the user_id also in the GROUP BY
AND if you only the highest 3 You have to add also LIMIT 3
A MySQL 5.x Solution to your problem
CREATE TABLE sumprice (
`id` INTEGER,
`name` VARCHAR(4),
`price` INTEGER,
`email` VARCHAR(9),
`user_id` INTEGER
);
INSERT INTO sumprice
(`id`, `name`, `price`, `email`, `user_id`)
VALUES
('1', 'Mike', '100', '@mike.com', '5'),
('2', 'John', '50', '@john.com', '6'),
('3', 'Mike', '100', '@mike.com', '5'),
('4', 'John', '50', '@john.com', '1'),
('5', 'Mark', '50', '@mark.com', '3'),
('6', 'Mike', '100', '@mike.com', '5');
SELECT @id:= @id +1 `id`, `name`, SUM(`price`) price , `email`, MAX(`user_id`)
FROM sumprice,(SELECT @id:=0) a
GROUP BY `email`, `name`
ORDER BY price DESC
id | name | price | email | MAX(`user_id`)
-: | :--- | ----: | :-------- | -------------:
1 | Mike | 300 | @mike.com | 5
2 | John | 100 | @john.com | 6
3 | Mark | 50 | @mark.com | 3
db<>fiddle here
In Mysql 8
CREATE TABLE sumprice (
`id` INTEGER,
`name` VARCHAR(4),
`price` INTEGER,
`email` VARCHAR(9),
`user_id` INTEGER
);
INSERT INTO sumprice
(`id`, `name`, `price`, `email`, `user_id`)
VALUES
('1', 'Mike', '100', '@mike.com', '5'),
('2', 'John', '50', '@john.com', '6'),
('3', 'Mike', '100', '@mike.com', '5'),
('4', 'John', '50', '@john.com', '1'),
('5', 'Mark', '50', '@mark.com', '3'),
('6', 'Mike', '100', '@mike.com', '5');
SELECT ROW_NUMBER() OVER(
ORDER BY SUM(price) DESC
) `id`, `name`, SUM(`price`) price , `email`, MAX(`user_id`)
FROM sumprice
GROUP BY `email`, `name`
ORDER BY price DESC
id | name | price | email | MAX(`user_id`)
-: | :--- | ----: | :-------- | -------------:
1 | Mike | 300 | @mike.com | 5
2 | John | 100 | @john.com | 6
3 | Mark | 50 | @mark.com | 3
db<>fiddle here
In Oracle it is the same
CREATE TABLE sumprice (
id NUMBER(5),
name VARCHAR(4),
price NUMBER(5),
email VARCHAR(9),
user_id NUMBER(5)
);
INSERT INTO sumprice
(id, name, price, email, user_id)
VALUES
('1', 'Mike', '100', '@mike.com', '5');
INSERT INTO sumprice
(id, name, price, email, user_id)
VALUES('2', 'John', '50', '@john.com', '6');
INSERT INTO sumprice
(id, name, price, email, user_id)
VALUES ('3', 'Mike', '100', '@mike.com', '5');
INSERT INTO sumprice
(id, name, price, email, user_id)
VALUES ('4', 'John', '50', '@john.com', '1');
INSERT INTO sumprice
(id, name, price, email, user_id)
VALUES ('5', 'Mark', '50', '@mark.com', '3');
INSERT INTO sumprice
(id, name, price, email, user_id)
VALUES ('6', 'Mike', '100', '@mike.com', '5');
SELECT ROW_NUMBER() OVER(
ORDER BY SUM(price) DESC
) id ,name, SUM(price) price , email, MAX(user_id)
FROM sumprice
GROUP BY email, name
ORDER BY price DESC
ID | NAME | PRICE | EMAIL | MAX(USER_ID)
-: | :--- | ----: | :-------- | -----------:
1 | Mike | 300 | @mike.com | 5
2 | John | 100 | @john.com | 6
3 | Mark | 50 | @mark.com | 3
db<>fiddle here