add tests and fix SQL statement