improved sqlite query
for sync.