-1

I have studied that DBMS won't evaluate the second condition in a WHERE clause if the first condition is met.

select *

from student

where name = 'ABC' or name ='XYZ';



select *

from student

where name = 'ABC' or 'XYZ';

The first returns all the rows with name 'ABC' and 'XYZ' while second one returns just the rows with name 'ABC'.

  • 1
    I think it should be `where name in ('ABC', 'XYZ');` – gawi Sep 21 '19 at 10:16
  • "I have studied that DBMS won't evaluate the second condition" Where? What authoritative documentation did you read? This is not so in SQL. PS `name = 'ABC' or 'XYZ'` parses `(name = 'ABC') or 'XYZ'` & is ORing a boolean & a string, not 2 booleans. – philipxy Sep 21 '19 at 20:05

2 Answers2

2

SQLite treats Boolean expressions as 0 for False and 1 for True.
Also converts implicitly string values to numbers when necessary in expressions like:

name = 'ABC' or 'XYZ'

in order to evaluate them.
So the above expression is equivalent to:

(name = 'ABC') or 'XYZ'

and so 'XYZ' is implicitly converted to the numeric value of 0 meaning False.
The result of this expression is:

(name = 'ABC') or False

which is equivalent to:

name = 'ABC'

This is why it returns just the rows with name 'ABC'

forpas
  • 160,666
  • 10
  • 38
  • 76
1

In second query XYZ is treated as condition(XYZ - false):

select *
from student
where name = 'ABC' or 'XYZ';

<=>

select *
from student
where name = 'ABC' or false;

<=>

select *
from student
where name = 'ABC';

"I have studied that DBMS won't evaluate the second condition in a WHERE clause if the first condition is met."

It depends, short-circuiting is not always the case.

I guess you want:

select *
from student
where name IN('ABC', 'XYZ'); -- basically the same as your first query
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275