Aggregated functions return something, even if nothing is found, simply because they implicitly group all (any) rows in your table to get you the cumulative value. Take count , for example:
sqlite> create table foo (a int not null); sqlite> select count(a) from foo; 0
null matches min , since 0 matches count , where no rows are returned.
This may be a useful property. Consider the following example:
sqlite> select ifnull(min(a), "I'm null") from foo; I'm null
If you want to filter out this case so that no records are returned, you can use the having clause (and do an explicit grouping):
sqlite> select min(a) as min_a from foo group by a having min_a not null;
Adam wagner
source share