I know that in oracle/mysql/sqlserver select statement you are not able to reference a column alias in the where clause because it is executed before the select, yet it works in sqlite3:
sqlite> create table t (c1 text, c2 text);
sqlite> insert into t values ("a1", "a2");
sqlite> insert into t values ("b1", "b2");
sqlite> select c1, c2 from t;
a1|a2
b1|b2
sqlite> select c1, c2 as alias_c2 from t where alias_c2='b2';
b1|b2
Why is this possible in sqlite?
Using sqlite3 with
SQLITE_DEBUGflag enabled:As can be seen from the instruction stack above, the loop over the rows (lines 8-23) repeats the
MultiplyandLecommands for each expression in thewhereclause, for each row in the table.So to answer my own question, sqlite engine is able to use the column aliases by substituting their definitions from the
selectat execution time of thewhere.