Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL returns result for an always false condition

Tags:

sql

sqlite

mysql

I'm confused on why the following SQL sentence returns one row:

#MySQL
select max(1) from dual where 0 = 1

#SQLite
select max(1) where 0 = 1 

I'm really confused about this result. Can anyone please shed some light over this behavior?

Thanks in advance!

like image 870
m0skit0 Avatar asked Jul 18 '26 22:07

m0skit0


2 Answers

An aggregate like max without a group by always returns one row. If the underlying rowset it empty, it will return a single row with null. Otherwise, it returns the maximum of the column you specified.

The where clause affects which rows the maximum is calculated for. It does not change the fact that the query has to return a maximum.

like image 50
Andomar Avatar answered Jul 20 '26 12:07

Andomar


Because MAX is a scalar function so it needs to return a result, as opposed to a table-result query.

like image 29
CyberDude Avatar answered Jul 20 '26 14:07

CyberDude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!