Optimized query for sqlite
jshint fixes