[sql] How to count occurrences of a column value efficiently in SQL?