<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="logical-operators">
      </a>
      14.4.3 Logical Operators
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045207007312">
  </a>
  <a class="indexterm" name="idm46045207006240">
  </a>
  <div class="table">
   <a name="idm46045207004752">
   </a>
   <p class="title">
    <b>
     Table 14.5 Logical Operators
    </b>
   </p>
   <div class="table-contents">
    <table frame="box" rules="all" summary="A reference that lists logical operators.">
     <colgroup>
      <col style="width: 28%"/>
      <col style="width: 71%"/>
     </colgroup>
     <thead>
      <tr>
       <th>
        Name
       </th>
       <th>
        Description
       </th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td>
        <a class="link" href="logical-operators.html#operator_and">
         <code class="literal">
          AND
         </code>
         ,
         <code class="literal">
          &amp;&amp;
         </code>
        </a>
       </td>
       <td>
        Logical AND
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="logical-operators.html#operator_not">
         <code class="literal">
          NOT
         </code>
         ,
         <code class="literal">
          !
         </code>
        </a>
       </td>
       <td>
        Negates value
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="logical-operators.html#operator_or">
         <code class="literal">
          OR
         </code>
         ,
         <code class="literal">
          ||
         </code>
        </a>
       </td>
       <td>
        Logical OR
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="logical-operators.html#operator_xor">
         <code class="literal">
          XOR
         </code>
        </a>
       </td>
       <td>
        Logical XOR
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  </div>
  <br class="table-break"/>
  <p>
   In SQL, all logical operators evaluate to
   <code class="literal">
    TRUE
   </code>
   ,
   <code class="literal">
    FALSE
   </code>
   , or
   <code class="literal">
    NULL
   </code>
   (
   <code class="literal">
    UNKNOWN
   </code>
   ). In MySQL,
        these are implemented as 1 (
   <code class="literal">
    TRUE
   </code>
   ), 0
        (
   <code class="literal">
    FALSE
   </code>
   ), and
   <code class="literal">
    NULL
   </code>
   . Most of
        this is common to different SQL database servers, although some
        servers may return any nonzero value for
   <code class="literal">
    TRUE
   </code>
   .
  </p>
  <p>
   MySQL evaluates any nonzero, non-
   <code class="literal">
    NULL
   </code>
   value
        to
   <code class="literal">
    TRUE
   </code>
   . For example, the following
        statements all assess to
   <code class="literal">
    TRUE
   </code>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa9165091"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">10</span> <span class="token operator">IS</span> <span class="token boolean">TRUE</span><span class="token punctuation">;</span>
<span class="token prompt">-&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">-</span><span class="token number">10</span> <span class="token operator">IS</span> <span class="token boolean">TRUE</span><span class="token punctuation">;</span>
<span class="token prompt">-&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token string">'string'</span> <span class="token operator">IS</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">-&gt;</span> <span class="token number">1</span></code></pre>
  </div>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <a name="operator_not">
     </a>
     <p>
      <a class="link" href="logical-operators.html#operator_not">
       <code class="literal">
        NOT
       </code>
      </a>
      ,
      <a class="link" href="logical-operators.html#operator_not">
       <code class="literal">
        !
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206966464">
     </a>
     <a class="indexterm" name="idm46045206965008">
     </a>
     <p>
      Logical NOT. Evaluates to
      <code class="literal">
       1
      </code>
      if the
            operand is
      <code class="literal">
       0
      </code>
      , to
      <code class="literal">
       0
      </code>
      if
            the operand is nonzero, and
      <code class="literal">
       NOT NULL
      </code>
      returns
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa59266743"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">NOT</span> <span class="token number">10</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">NOT</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">!</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">!</span> <span class="token number">1</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span></code></pre>
     </div>
     <p>
      The last example produces
      <code class="literal">
       1
      </code>
      because the
            expression evaluates the same way as
      <code class="literal">
       (!1)+1
      </code>
      .
     </p>
     <p>
      The
      <a class="link" href="logical-operators.html#operator_not">
       <code class="literal">
        !
       </code>
      </a>
      operator
            is a nonstandard extension, and is deprecated; expect it to
            be removed in a future version of MySQL. Applications, where
            necessary, should be adjusted to use the standard SQL
      <a class="link" href="logical-operators.html#operator_not">
       <code class="literal">
        NOT
       </code>
      </a>
      operator instead.
     </p>
    </li>
    <li class="listitem">
     <a name="operator_and">
     </a>
     <p>
      <a class="link" href="logical-operators.html#operator_and">
       <code class="literal">
        AND
       </code>
      </a>
      ,
      <a class="link" href="logical-operators.html#operator_and">
       <code class="literal">
        &amp;&amp;
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206942784">
     </a>
     <a class="indexterm" name="idm46045206941328">
     </a>
     <p>
      Logical AND. Evaluates to
      <code class="literal">
       1
      </code>
      if all
            operands are nonzero and not
      <code class="literal">
       NULL
      </code>
      , to
      <code class="literal">
       0
      </code>
      if one or more operands are
      <code class="literal">
       0
      </code>
      , otherwise
      <code class="literal">
       NULL
      </code>
      is
            returned.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa45032263"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token operator">AND</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token operator">AND</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token operator">AND</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">0</span> <span class="token operator">AND</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token boolean">NULL</span> <span class="token operator">AND</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span></code></pre>
     </div>
     <p>
      The
      <a class="link" href="logical-operators.html#operator_and">
       <code class="literal">
        &amp;&amp;
       </code>
      </a>
      ,
            operator is a nonstandard extension and is deprecated;
            expect support for it to be removed in a future version of
            MySQL. Applications, where necessary, should be adjusted to
            use the standard SQL
      <a class="link" href="logical-operators.html#operator_and">
       <code class="literal">
        AND
       </code>
      </a>
      operator instead.
     </p>
    </li>
    <li class="listitem">
     <a name="operator_or">
     </a>
     <p>
      <a class="link" href="logical-operators.html#operator_or">
       <code class="literal">
        OR
       </code>
      </a>
      ,
      <a class="link" href="logical-operators.html#operator_or">
       <code class="literal">
        ||
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206920880">
     </a>
     <a class="indexterm" name="idm46045206919424">
     </a>
     <p>
      Logical OR. When both operands are
            non-
      <code class="literal">
       NULL
      </code>
      , the result is
      <code class="literal">
       1
      </code>
      if any operand is nonzero, and
      <code class="literal">
       0
      </code>
      otherwise. With a
      <code class="literal">
       NULL
      </code>
      operand, the result is
      <code class="literal">
       1
      </code>
      if the other operand is nonzero, and
      <code class="literal">
       NULL
      </code>
      otherwise. If both operands are
      <code class="literal">
       NULL
      </code>
      , the result is
      <code class="literal">
       NULL
      </code>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa69354765"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token operator">OR</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token operator">OR</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">0</span> <span class="token operator">OR</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">0</span> <span class="token operator">OR</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token operator">OR</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span></code></pre>
     </div>
     <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
      <div class="admon-title">
       Note
      </div>
      <p>
       If the
       <a class="link" href="sql-mode.html#sqlmode_pipes_as_concat">
        <code class="literal">
         PIPES_AS_CONCAT
        </code>
       </a>
       SQL mode is enabled,
       <a class="link" href="logical-operators.html#operator_or">
        <code class="literal">
         ||
        </code>
       </a>
       signifies
              the SQL-standard string concatenation operator (like
       <a class="link" href="string-functions.html#function_concat">
        <code class="literal">
         CONCAT()
        </code>
       </a>
       ).
      </p>
     </div>
     <p>
      The
      <a class="link" href="logical-operators.html#operator_or">
       <code class="literal">
        ||
       </code>
      </a>
      , operator
            is a nonstandard extension, and is deprecated; expect
            support for it to be removed in a future version of MySQL.
            Applications, where necessary, should be adjusted to use the
            standard SQL
      <a class="link" href="logical-operators.html#operator_or">
       <code class="literal">
        OR
       </code>
      </a>
      operator
            instead. Exception: Deprecation does not apply if
      <a class="link" href="sql-mode.html#sqlmode_pipes_as_concat">
       <code class="literal">
        PIPES_AS_CONCAT
       </code>
      </a>
      is enabled
            because, in that case,
      <a class="link" href="logical-operators.html#operator_or">
       <code class="literal">
        ||
       </code>
      </a>
      signifies
            string concatenation.
     </p>
    </li>
    <li class="listitem">
     <a name="operator_xor">
     </a>
     <p>
      <a class="link" href="logical-operators.html#operator_xor">
       <code class="literal">
        XOR
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206890832">
     </a>
     <p>
      Logical XOR. Returns
      <code class="literal">
       NULL
      </code>
      if either
            operand is
      <code class="literal">
       NULL
      </code>
      . For
            non-
      <code class="literal">
       NULL
      </code>
      operands, evaluates to
      <code class="literal">
       1
      </code>
      if an odd number of operands is
            nonzero, otherwise
      <code class="literal">
       0
      </code>
      is returned.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa88455011"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">XOR</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">0</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">XOR</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">XOR</span> <span class="token boolean">NULL</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token number">1</span> <span class="token keyword">XOR</span> <span class="token number">1</span> <span class="token keyword">XOR</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span></code></pre>
     </div>
     <p>
      <code class="literal">
       a XOR b
      </code>
      is mathematically equal to
      <code class="literal">
       (a AND (NOT b)) OR ((NOT a) and b)
      </code>
      .
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
