title: SQL GROUP BY before JOIN - sequence when querying
body: <pre><code>SELECT 
      Income.point, Income.date, SUM(out), SUM(inc)
  FROM 
      Income 
  LEFT JOIN
      Outcome ON Income.point = Outcome.point 
             AND Income.date = Outcome.date
  GROUP BY 
      Income.point, Income.date
  
  UNION
  
  SELECT 
      Outcome.point, Outcome.date, SUM(out), SUM(inc)
  FROM 
      Outcome 
  LEFT JOIN
      Income ON Income.point = Outcome.point 
             AND Income.date = Outcome.date
  GROUP BY 
      Outcome.point, Outcome.date;
  </code></pre>
  <p>I have this code what I want to do is to group by before joining.
  &quot;Assume that we have an SQL query containing joins and a group-by. The standard way of evaluating this type of query is to first perform all the joins and then the group-by operation. However, it may be possible to perform the group-by early, that is, to push the group-by operation past one or more joins. Early grouping may reduce the query processing cost by reducing the amount of data participating in joins.&quot;
  So I need explanation how to do that</p>
  <p>exercise is as follows in this case :</p>
  <blockquote>
  <p>Under the assumption that the income (inc) and expenses (out) of the money at each outlet (point) are registered any number of times a day, get a result set with fields: outlet, date, expense, income.</p>
  <p>Note that a single record must correspond to each outlet at each date.</p>
  <p>Use Income and Outcome tables.</p>
  </blockquote>