sqlsqlite

SELECT COUNT(*), with missing FROM doesn't fail, what is it doing?


I had acidentially written this code

SELECT COUNT(*) "Table0" WHERE "Column0" = ? LIMIT 1;

When I meant to write this code:

SELECT COUNT(*) FROM "Table0" WHERE "Column0" = ? LIMIT 1;

I was really surprised that the first line compiled without giving any kind of error. I have checked the syntax on in the SQLite docs, and it doesn't seem like this should compile, but it does. What is going on here? How is this getting interpreted by SQLite?

The incorrect line always returns 0, even if there is a match in the table.

Also, interesting, is that LibSQL actually does fail, but with a message about "Column0" not existing... perhaps this will make more sense once we understand how it is getting interpreted...


Solution