title: Minimizing SQL queries using join with one-to-many relationship
body: <p>So let me preface this by saying that I'm not an SQL wizard by any means. What I want to do is simple as a concept, but has presented me with a small challenge when trying to minimize the amount of database queries I'm performing.</p>
  
  <p>Let's say I have a table of departments. Within each department is a list of employees.</p>
  
  <p>What is the most efficient way of listing all the departments and which employees are in each department.</p>
  
  <p><strong>So for example</strong> if I have a department table with:</p>
  
  <pre><code>id   name
  1    sales
  2    marketing
  </code></pre>
  
  <p>And a people table with:</p>
  
  <pre><code>id   department_id   name
  1    1               Tom
  2    1               Bill
  3    2               Jessica
  4    1               Rachel
  5    2               John
  </code></pre>
  
  <p>What is the best way list all departments and all employees for each department like so:</p>
  
  <p><strong>Sales</strong></p>
  
  <ul>
  <li>Tom </li>
  <li>Bill</li>
  <li>Rachel</li>
  </ul>
  
  <p><strong>Marketing</strong></p>
  
  <ul>
  <li>Jessica</li>
  <li>John</li>
  </ul>
  
  <p>Pretend both tables are actually massive. (I want to avoid getting a list of departments, and then looping through the result and doing an individual query for each department). Think similarly of selecting the statuses/comments in a Facebook-like system, when statuses and comments are stored in separate tables.</p>