title: Improve query of getting row with max value
body: <p>I have next tables:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `Customers` (
      `id` INT AUTO_INCREMENT,
      `name` VARCHAR(20) NOT NULL,
      PRIMARY KEY(`id`)
  );
  
  CREATE TABLE IF NOT EXISTS `Orders` (
      `id` INT AUTO_INCREMENT,
      `id_cust` INT NOT NULL,
      `descr` VARCHAR(40),
      `price` INT NOT NULL,
      PRIMARY KEY(`id`),
      FOREIGN KEY(`id_cust`) REFERENCES `Customers`(`id`)
  );
  </code></pre>
  
  <p>One customer can have many orders. I want to get <code>id_cust</code> and sum of the orders of who paid the most(one person).
  My query:</p>
  
  <pre><code>SELECT cust, max_orders_sum
  FROM
  (
      (
          SELECT MAX(orders_sum) AS max_orders_sum
          FROM (
              SELECT o.id_cust AS cust, SUM(o.price) AS orders_sum
              FROM Orders AS o
              GROUP BY o.id_cust
          ) AS same_query0
      ) AS step1
      INNER JOIN
      (
          SELECT o.id_cust AS cust, SUM(o.price) AS orders_sum 
          FROM Orders AS o
          GROUP BY o.id_cust
      ) AS same_query1
      ON (step1.max_orders_sum = same_query1.orders_sum)
  );
  </code></pre>
  
  <p><strong>Main problem:</strong>
  as you can see, it has the same parts: <code>same_query0</code> and <code>same_query1</code>. Is there any way to get rid of them?
  Or if you know the better way to reach my goal, please share.
  I found one simple solution:</p>
  
  <pre><code>SELECT o.id_cust AS cust, SUM(o.price) AS orders_sum
  FROM Orders AS o
  GROUP BY o.id_cust
  ORDER BY orders_sum DESC LIMIT 1;
  </code></pre>
  
  <p>But this is not a direct way to solve the problem.</p>