Retrieving Information from a Table You need not retrieve an entire table when you use  COUNT() . For example, the previous query, when performed just on dogs and cats, looks like this: Or, if you wanted the number of animals per sex only for animals whose sex is known: If you name columns to select in addition to the  COUNT()  value, a  GROUP BY  clause should be present that names those same columns. Otherwise, the following occurs: See also Section 12.20.3, “MySQL Handling of GROUP BY” . See Section 12.20.1, “Aggregate Function Descriptions”  for information about  COUNT( expr )  behavior and related optimizations. mysql>  SELECT species, sex, COUNT(*) FROM pet WHERE species = ’dog’ OR species = ’cat’ GROUP BY species, sex; +---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | +---------+------+----------+ +---------+------+----------+ | species | sex | COUNT(*) | +---------+------+----------+ | bird | f | 1 | | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | | hamster | f | 1 | | snake | m | 1 | +---------+------+----------+ mysql>  SELECT species, sex, COUNT(*) FROM pet WHERE sex IS NOT NULL GROUP BY species, sex; mysql>  SET sql_mode = ’ONLY_FULL_GROUP_BY’; Query OK, 0 rows affected (0.00 sec) mysql>  SELECT owner, COUNT(*) FROM pet; ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column ’menagerie.pet.owner’; this is incompatible with sql_mode=only_full_group_by mysql>  SET sql_mode = ’’; Query OK, 0 rows affected (0.00 sec) mysql>  SELECT owner, COUNT(*) FROM pet; +--------+----------+ | owner | COUNT(*) | +--------+----------+ | Harold | 8 | +--------+----------+ 1 row in set (0.00 sec) •If the  ONLY_FULL_GROUP_BY  SQL mode is enabled, an error occurs: •If  ONLY_FULL_GROUP_BY  is not enabled, the query is processed by treating all rows as a single group, but the value selected for each named column is nondeterministic. The server is free to select the value from any row: 3.3.4.9 Using More Than one Table 323