title: Inner join with count and group by
body: <p>I have 2 tables</p>
  
  <p><strong>Timetable :</strong><br>
  pupil_id, staff_id, subject, lesson_id</p>
  
  <p><strong>Staff_info :</strong><br>
  staff_id, surname</p>
  
  <p>The timetable table contains 1000s of rows because each student's ID is listed under each period they do.</p>
  
  <p>I want to list all the teacher's names, and the number of lessons they do (count). So I have to do SELECT with DISTINCT.</p>
  
  <pre><code>SELECT DISTINCT TIMETABLE.STAFF_ID,
                  COUNT(TIMETABLE.LESSON_ID),
                  STAFF.SURNAME
  FROM STAFF
  INNER JOIN TIMETABLE ON TIMETABLE.STAFF_ID = STAFF.STAFF_ID
  GROUP BY TIMETABLE.STAFF_ID
  </code></pre>
  
  <p>However I get the error:</p>
  
  <blockquote>
    <p>Column 'STAFF.SURNAME' is invalid in the select list because it is not
    contained in either an aggregate function or the GROUP BY clause.</p>
  </blockquote>