Remove unnecessary query.count() calls by using first()

Since query.first() returns None if there are no rows in the table, there is no
need to count the rows first. Discussed in pull #1.