Message counter using SQL query.