I’m working on simple queries to learn MySQL, in my example database, I keep track of Stores which sells electronic devices, I have a table Sells(Store, Item, Price).
And example data is,
'Best Buy', 'Galaxy S', 1000
'Buy More', 'Macbook Air', 2000
'Best Buy', 'Microsoft Mouse', 20
'Best Buy', 'Macbook Pro Cover', 40
'Buy More', 'Asus Zenbook', 2000
And so on..
I tried the following sql statement, but it says:
Error Code: 1111. Invalid use of group function 0.000 sec
SELECT store
FROM sells
WHERE SUM(price) <
(SELECT SUM(price) AS total
FROM sells
GROUP BY store
ORDER BY total DESC
LIMIT 1)
GROUP BY store
ORDER BY SUM(price) DESC
I would be appreciate if you can help me
Thanks
This will just plain show the second most expensive store;
Demo here.
If you want the price displayed too, you can just select that too;
Demo here.
Edit: If you have several most expensive stores and several second most expensive stores, the query to get the all the second most expensive ones becomes quite a bit more convoluted; I’m sure someone can beat the efficiency of this one;
Demo here.