title: Finding the 2nd most expensive total products in MySQL
body: <p>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).</p>
  
  <p>And example data is,</p>
  
  <pre><code>'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
  </code></pre>
  
  <p>And so on..</p>
  
  <p>I tried the following sql statement, but it says:</p>
  
  <blockquote>
    <p>Error Code: 1111. Invalid use of group function   0.000 sec</p>
  </blockquote>
  
  <pre><code>SELECT store
  FROM sells
  WHERE SUM(price) &lt;
          (SELECT SUM(price) AS total
          FROM sells
          GROUP BY store
          ORDER BY total DESC
          LIMIT 1) 
  GROUP BY store
  ORDER BY SUM(price) DESC
  </code></pre>
  
  <p>I would be appreciate if you can help me</p>
  
  <p>Thanks</p>