Typically, lexers will add characters to the current token until they find a character that cannot belong to the current token, then it terminates and starts from the point where it could not continue.
So what is happening here is that the lexer takes away the SELECT and sees that the next character is * , which, as it collects the word, cannot belong to SELECT . So it stops, parses the SELECT , which turns out to be a keyword, and starts with * , which it recognizes, and so on. This is the same reason you get 4 from 2*2 and 2 * 2 in other programming languages.
As for whether it will work in other databases, it all depends on the details of the lexical analyzer and the rules of grammar.
source share