title: SQL COUNT query returning NULL when using temporary table
body: <p>I'm using the following code to try to return the average revenue based on the number of songs in an album.</p>
  <pre><code>WITH Album_Tracks AS (
     SELECT COUNT(*) AS &quot;#ofTracks&quot;, SUM(invoice_items.UnitPrice) AS &quot;Album_Revenue&quot;
     from tracks
     LEFT JOIN invoice_items ON invoice_items.trackID = tracks.trackID
     GROUP BY Albumid
  )
  
  SELECT #ofTracks, AVG(Album_Revenue)
  FROM Album_Tracks
  GROUP BY #ofTracks
  ORDER BY #ofTracks DESC
  </code></pre>
  <p>The problem I am running into is that the &quot;# of tracks&quot; column in the temporary table 'Album_Tracks' returns a column of NULL values when i don't select all data from the table.</p>
  <p>As in, doing this: <code>SELECT * FROM Album_Tracks</code> returns values, but <code>SELECT #ofTracks FROM Album_Tracks</code> returns only null values.</p>
  <p>I'm not sure what the problem in my code is.</p>
  <p>Expecting to see:
  <a href="https://i.stack.imgur.com/BL1Pw.png" rel="nofollow noreferrer">Example 1</a></p>
  <p>But am getting:
  <a href="https://i.stack.imgur.com/cM6BH.png" rel="nofollow noreferrer">Example 2</a></p>
  <p>First example the order by was not included.</p>