Suppose you issue a simple SELECT statement against the PUBS database:
SELECT State
FROM Authors
The result is 23 rows of states, ranging from California (15 instances) to Utah (2 instances). Each of the other states represented appears once.
Suppose you wanted to show the count of each state in the table. This is where grouping comes in. If you can group by state, then count the states returned, correct? That is exactly what you will do. Here is the statement that will do the trick:
SELECT State, count(State)
FROM Authors
GROUP BY State
Two things are happening here.
First, you indicate the column (State) and table (Authors) that you want to use.
By using the COUNT function, SQL will return the count of all rows returned for that column.
By adding the GROUP BY clause, SQL will condense the rows that have the same state, eliminating duplicates:
The GROUP BY clause will condense the rows with the same state, eliminating duplicates.
Note that the second column has no heading. This is because the column is calculated on the fly, without specifying a column heading. Your application may label the column with NULL, No Column Heading, or some other indication that the column heading was not provided.
Using SQL SUM function Example
Let us look at a SQL GROUP BY query example that uses the SQL SUM function.
This GROUP BY example uses the SUM function to return the name of the department and the total sales (for the department).
SELECT department, SUM(sales) AS "Total sales"
FROM order_details
GROUP BY department;
Because you have listed one column (the department field) in your SQL SELECT statement that is not encapsulated in the SUM function, you must use the GROUP BY Clause. The department field must, therefore, be listed in the GROUP BY clause.
SQL GROUP BY function
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name WHERE
column_name operator value
GROUP BY column_name;