Note that there are some explanatory texts on larger screens.

plurals
  1. POWHERE condition issue in SQL
    primarykey
    data
    text
    <p>I am facing problem with an SQL query:</p> <p>Here is my table data:</p> <pre><code>+------------+--------+--------+------------+------------+ | priceId_PK | mPrice | aPrice | validFrom | validTo | +------------+--------+--------+------------+------------+ | 1 | 0.00 | 0.00 | 2013-07-01 | 2013-08-31 | | 2 | 7.50 | 2.50 | 2013-09-01 | 2013-12-31 | | 3 | 15.00 | 5.00 | 2014-01-01 | 2035-12-31 | +------------+--------+--------+------------+------------+ </code></pre> <p>And my SQL query is</p> <pre><code>SELECT mPrice, aPrice, CASE WHEN validFrom &lt; '2013-11-01' THEN '2013-11-01' ELSE validFrom END AS validFrom, CASE WHEN validTo &gt; '2013-11-30' THEN '2013-11-30' ELSE validTo END AS validTo FROM commission WHERE (validfrom BETWEEN '2013-11-01' AND '2013-11-30' OR validto BETWEEN '2013-11-01' AND '2013-11-30') ORDER BY validFrom </code></pre> <p>My expected result:</p> <pre><code>+--------+--------+------------+------------+ | mPrice | aPrice | validFrom | validTo | +--------+--------+------------+------------+ | 7.50 | 2.50 | 2013-11-01 | 2013-11-30 | +--------+--------+------------+------------+ </code></pre> <p>But the query actually returns an empty set.</p> <p>Without the <code>WHERE</code> condition, the query is</p> <pre><code>SELECT mPrice, aPrice, CASE WHEN validFrom &lt; '2013-11-01' THEN '2013-11-01' ELSE validFrom END AS validFrom, CASE WHEN validTo &gt; '2013-11-30' THEN '2013-11-30' ELSE validTo END AS validTo FROM commission </code></pre> <p>This returns:</p> <pre><code>+--------+--------+------------+------------+ | mPrice | aPrice | validFrom | validTo | +--------+--------+------------+------------+ | 0.00 | 0.00 | 2013-11-01 | 2013-08-31 | | 7.50 | 2.50 | 2013-11-01 | 2013-11-30 | | 15.00 | 5.00 | 2014-01-01 | 2013-11-30 | +--------+--------+------------+------------+ </code></pre> <p>My question is, Why does the query with the <code>WHERE</code> condition return empty data when I expect it to return a single result set?</p> <p>Is it a problem with <a href="https://stackoverflow.com/questions/20425808/sql-execution-order">the execution order of <code>SELECT</code> and <code>WHERE</code> conditions</a>?</p> <p>Or is it another issue with <code>WHERE</code> condition that is not working properly?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload