This should do the trick:
SELECT SUM(cost) AS cost FROM Books WHERE ClassId=? GROUP BY ClassId
Where the question makr is either the ID of the class or part of a prepared statement where you feed it the class ID.
You can retrieve the sum with column name "cost".