title: SQL that matches all or nothing
body: <p>I have the following table in PostgreSQL database:</p>
  
  <pre><code>CREATE TABLE maclist (
      username character varying,
      mac macaddr NOT NULL,
      CONSTRAINT maclist_user_mac_key UNIQUE (username, mac)
  );
  </code></pre>
  
  <p>I need a way to check if the MAC address is assigned to the user or the user has no assigned MAC addresses at all. Basically I need a query that returns row if all conditions are true or no condition is true ie <strong>NOT a XOR b</strong>.</p>
  
  <p><strong>EDIT:</strong>
  Example:</p>
  
  <pre><code>username | mac
  john     | 11:22:33:44:55:66
  john     | 11:22:33:44:55:67
  doe      | 11:22:33:44:55:68
  </code></pre>
  
  <p>If I query:</p>
  
  <pre><code>username = john, mac = 11:22:33:44:55:66                     -&gt; true, 1 whatever...
  username = john, mac != 11:22:33:44:55:66                    -&gt; 0, null or nothing...
  username = jane, mac = no matter what except john's or doe's -&gt; true, 1 whatever...
  username = jane, mac = john's or doe's                       -&gt;  0, null or nothing...
  </code></pre>
  
  <p>I need <code>true</code> under two conditions:</p>
  
  <ol>
  <li>There is a row for that (user, mac) combination. There are no rows</li>
  <li>for that user AND there are no rows for that mac</li>
  </ol>
  
  <p>So far I got this:</p>
  
  <pre><code>SELECT yes
  FROM
    (SELECT 1 AS yes) AS dummy
  LEFT JOIN maclist ON (username = 'user'
                        OR mac = '11:22:33:44:55:66')
  WHERE ((username = 'user'
          AND mac = '11:22:33:44:55:66')
         OR (username IS NULL
             AND mac IS NULL);
  </code></pre>
  
  <p>It works, but it seems to me like a hack and I also have no idea about the performance of this query as the database grows.
  My question is if there are any better ways to do this.</p>