While trying to isolate the 7 rows in the first query below, I got results that, as a novice, I was not expecting. I read over this SQLite document but do not understand why the 7 rows which are NULL are separated in the GROUP BY in the first query, but testing on != '', not in ('A','H'), and ='' all exclude the NULL rows.
It seems as though the tests are exclusive, such that NULL is either ='' or !='', or in ('A','H') or not in ('A','H'). It appears to be ignored by all of these, yet is separated in the GROUP BY.
Would you please explain why it works this way?
Thank you.
sqlite> select substr(trim(grammarCode),1,1) as c, count(indexRow) as cnt
from tbl
group by c
order by cnt;
c cnt
- ------
7
A 4828
20046
H 300679
sqlite> select substr(trim(grammarCode),1,1) as c, count(indexRow) as cnt
from tbl
where c != ''
group by c
order by cnt;
c cnt
- ------
A 4828
H 300679
sqlite> select substr(trim(grammarCode),1,1) as c, count(indexRow) as cnt
from tbl
where c not in ('A', 'H')
group by c
order by cnt;
c cnt
- ------
20046
sqlite> select substr(trim(grammarCode),1,1) as c, count(indexRow) as cnt
from tbl
where c = ''
group by c
order by cnt;
c cnt
- ------
20046
sqlite> select substr(trim(grammarCode),1,1) as c, count(indexRow) as cnt
from tbl
where c is null
group by c
order by cnt;
c cnt
- ------
7
A boolean expression in SQL evaluates as
trueorfalseornull.The
WHEREclause of any SQL statement filters out all rows for which the boolean expression/condition is nottrue, meaningfalseandnullare filtered out.All these boolean expressions:
are evaluated as
null(demo), because any comparison of/tonullwithout the use of the operatorISreturnsnull.This is why your 2nd, 3d and 4th queries filter out not only the rows that don't satisfy the condition in the
WHEREclause but also the rows wherecisnull.If you want these rows where
cisnullyou must explicitly mention that:Or, for the first 2 cases you can use the operator
IS: