I'm trying to understand this code from: Standard Deviation for SQLite.
create table t (row int);
insert into t values (1),(2),(3);
SELECT AVG((t.row - sub.a) * (t.row - sub.a)) as var from t,
(SELECT AVG(row) AS a FROM t) AS sub;
0.666666666666667
It computes the variance of the given values (I'm aware it doesn't compute the unbiased estimator, this is not at issue).
I am having a hard time searching for "Sqlite AS" on search engines as you might imagine... please suggest better search terms if you know any. In any case, how is this expression parsed? I'm not familiar with putting commas between select statements like
SELECT ..., SELECT ...;
In order for the the expression to make sense, sub.a
must be defined before the first select statement can be executed. Does this mean that Sqlite is parsing these statements right-to-left? Or does it parse left-to-right and then realize that the second one needs to be evaluated first? I'm also confused about why sub
does not appear printed. Is the results of a
SELECT ..., SELECT ...;
only the result of the first select statement?