title: how to use aggregate functions subquery in the main query
body: <p>This is my database schema </p>
  
  <pre><code>Horse(horseId, horseName, age, gender, registration, stableId)
  Owner(ownerId, lname, fname)
  Owns(horseId, ownerId)
  Stable(stableId, stableName, location, colors)
  Trainer(trainerId, lname, fname, stableId)
  Race(raceId, raceName, trackName, raceDate, raceTime)
  RaceResults(raceId, horseId, results, prize)
  Track(trackName, location, length)
  </code></pre>
  
  <p>I want to write this as a query:</p>
  
  <p>For every horse that has run more than three races, list the horse name, total winnings and number of races run for each horse. The column names should be "Horse Name", "Winnings", and "Races". The result should be sorted by the winnings in descending order.</p>
  
  <p>I've written this subquery as a beginning:</p>
  
  <pre><code>SELECT horseid, COUNT( horseid ) AS NumberOfRaces, SUM( prize ) winnings
  FROM raceresults
  GROUP BY horseid
  HAVING COUNT( horseid ) &gt;3
  </code></pre>
  
  <p>But how can I use the aggregate functions results {NumberOfRaces and winnings} in the main query?</p>