Using SQL, how do I write a query that returns a null if there are no entries, but lists all entries if any exist?
<p>Here's an example of what I mean.  Imagine you have a database with a table in it that has people and the cars they own.  Some people own more than one car, some people own no cars at all.  So your table might look something like this:</p>

<pre><code>+----+---------------+-------------+------------+
| id | name          | car_id      |department  |
+----+---------------+-------------+------------+
|  1 | John Smith    |           0 |          1 |
|  2 | Josey Wales   |           1 |          1 |
|  3 | Sally Thomas  |           2 |          2 |
|  3 | Bob McClain   |        null |          2 |
|  4 | Peter Sellers |        null |          3 |
+----+---------------+-------------+------------+
</code></pre>

<p>I need to write a query that would return the department number and all car IDs that belong to someone in that department.  The tricky part is that if there are no cars in the department, you should still list the department number followed by null.  But if there are cars, then you don't need to list null at all even if there are employees with a null entry in their car id.  So the output would be something like this.</p>

<pre><code>+------------+-----------+
|department  | car_id    |
+------------+-----------+
|          1 |         0 |
|          1 |         1 |
|          2 |         2 |
|          3 |      null |
+------------+-----------+
</code></pre>

<p>Note that the result should NOT be this -</p>

<pre><code>+------------+-----------+
|department  | car_id    |
+------------+-----------+
|          1 |         0 |
|          1 |         1 |
|          2 |         2 |
|          2 |      null |
|          3 |      null |
+------------+-----------+
</code></pre>

<p>or this</p>

<pre><code>+------------+-----------+
|department  | car_id    |
+------------+-----------+
|          1 |         0 |
|          1 |         1 |
|          2 |         2 |
+------------+-----------+
</code></pre>

<p>I'm having trouble figuring out how to get the null if there are no car values, but ignore it if there's at least one car that can be listed.  I'm kind of a beginner to SQL and I really appreciate your help!  Also, if it matters, I'm using MySQL.</p>