title: How do I optimize this query?
body: <p>The following query gets the info that I need. However, I noticed that as the tables grow, my code gets slower and slower. I'm guessing it is this query. Can this written a different way to make it more efficient?  I've heard a lot about using joins instead of subqueries, however, I don't "get" how to do it.</p>
  
  <pre><code>  SELECT * FROM
  
    (SELECT MAX(T.id) AS MAXid
    FROM transactions AS T 
    GROUP BY T.position
    ORDER BY T.position) AS result1,
  
    (SELECT T.id AS id, T.symbol, T.t_type, T.degree, T.position, T.shares, T.price, T.completed, T.t_date,
    DATEDIFF(CURRENT_DATE, T.t_date) AS days_past, 
    IFNULL(SUM(S.shares), 0) AS subtrans_shares,
    T.shares - IFNULL(SUM(S.shares),0) AS due_shares,
  
    (SELECT IFNULL(SUM(IF(SO.t_type = 'sell', -SO.shares, SO.shares )), 0) 
    FROM subtransactions AS SO WHERE SO.symbol = T.symbol) AS owned_shares
  
    FROM transactions AS T
    LEFT OUTER JOIN subtransactions AS S
    ON T.id = S.transid     
    GROUP BY T.id
    ORDER BY T.position) AS result2
  
    WHERE MAXid = id
  </code></pre>