The first query checks whether the result has to be -1. If not, the second query is run and returns the actual result.