The following syntax will work for most SQL engines:
SELECT expression1, expression2, ... expression_n,
COUNT(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
[ORDER BY expression [ ASC | DESC ]];
Parameters or Arguments
expression1, expression2, ... expression_n
Expressions
that are not encapsulated within the COUNT function and must be
included in the GROUP BY clause at the end of the SQL statement.
aggregate_expression
This is the column or expression whose non-null values will be counted.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. These are conditions that must be met for the records to be selected.
ORDER BY expression
Optional. The expression used to sort the records in the result set. If more than one expression is
provided, the values should be comma separated.
ASC
Optional. ASC sorts the result set in ascending order by expression. This is the default behavior, if no modifier is
provider.
DESC
Optional. DESC sorts the result set in descending order by expression.
1
Refer to documentation for the specific engine used:
...etc
For the "question"
I have a SQL table with three columns: name, date of birth, and current state. I want to return a result-set with the number of times each state appeared in the original table. The result-set will have 2 columns: state and count.
such a query could be constructed like this:
SELECT current_state as state, count(*) as count
FROM data
GROUP BY current_state
See an example of this in this SQLfiddle.
1https://www.techonthenet.com/sql/count.php