I am learning sqlite using the example from sqldf, I have come across example 3 about nested select and I don’t quite understand the code:
sqldf("select iris.Species '[Species]',
avg(Sepal_Length) '[Avg of SLs > avg SL]'
from iris,
(select Species, avg(Sepal_Length) SLavg
from iris group by Species) SLavg
where iris.Species = SLavg.Species
and Sepal_Length > SLavg
group by iris.Species")
I think the code above is trying to:
- create a table with
[Species]and[Avg of SLs > avg SL]using data fromiris(from line 1-3) - create a table named
SLavgwith Species and average Sepal_Length for each Species fromiris(from line 4-6)
I wonder why SLavg is present in line 4-5 for 2 times, I tried removing SLavg and sqldf gives me statement: no such column: SLavg and statement: no such column: SLavg.Species.
The code queries two columns from cross-join of two tables:
Where T2 is a subquery which is given shorthand ‘SLavg’:
SLavg is used for both subquery name and column name, and that’s confusing. Let’s give them different names:
Now it should be more understandable:
AVGtblproduces two columns (Species,AVGcol)irisandAVGtblproduces four columns (iris.Species,iris.Sepal_Length,AVGtbl.Species,AVGtbl.AVGcol).The
whereclause of top-levelselectoperates on these four.