title: GROUP BY table1.column_name ORDER BY table2.column_name
body: <p>I have a table of posts, and a table of users, and I have a table in-between that has the relationships between the two. One user can have many posts etc.</p>
  
  <p>The posts can be 'starred' by adding a 0 or 1 into the starred column on posts table. Only one post will be starred per user, but some users will have no starred posts.</p>
  
  <p>I need to create a query that pulls one post per user (so I am using 'group by'), and of those posts it should get the starred one, not all users will have a starred post so I can't just use </p>
  
  <pre><code>WHERE starred = 1
  </code></pre>
  
  <p>Essentially I want to order the group by starred so it gets the starred post if there is one and a normal one if there isn't. </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>SELECT
     posts.post_title
  FROM
     posts
  INNER JOIN rel_company_post ON posts.post_id = rel_company_post.post_id
  INNER JOIN companies ON rel_company_post.company_id = companies.company_id
  GROUP BY companies.company_id
  ORDER BY posts.starred DESC
  </code></pre>