title: SQL Command Not Properly Ended (Nested Aggregation with Group-by)
body: <p>I keep getting this error when I tried to execute this query, although I couldn't figure out what went wrong. I'm using Oracle and JDBC.</p>
  
  <p>Here's the query:</p>
  
  <pre><code>SELECT Temp.flight_number, Temp.avgprice 
  FROM (SELECT P.flight_number, AVG (P.amount) AS avgprice 
          FROM purchase P 
         GROUP BY P.flight_number) AS Temp 
  WHERE Temp.avgprice = (SELECT MAX (Temp.avgprice) 
                           FROM Temp)
  </code></pre>
  
  <p>I'm trying to get the maximum of average price of the tickets that customers have booked, group by flight_number.</p>