Select Count(*)
From (
Select ...
From TempTable
Group By column_1, column_2, column_3, column_4
) As Z
SELECT country, COUNT(*) AS customers
FROM Customers
GROUP BY country;
# To count the number of teachers in each department, including
# rows where COUNT(t.name) == 0
SELECT d.name,COUNT(t.name) FROM teacher t
RIGHT JOIN dept d ON (t.dept=d.id) GROUP BY d.name;