skip to main content
Supported SQL Functionality : Select : Select Clause : Group By Clause
  

Try DataDirect Drivers Now
Group By Clause

Purpose

Specifies the names of one or more columns by which the returned values are grouped. This clause is used to return a set of aggregate values.

Syntax

GROUP BY column_expression[,...]
where:
column_expression
is either a column name or a SQL expression. Multiple values must be separated by a comma. If column_expression is a column name, it must match one of the column names specified in the Select clause. Also, the Group By clause must include all non-aggregate columns specified in the Select list.

Example

The following example totals the salaries in each department:
SELECT dept_id, sum(salary) FROM emp GROUP BY dept_id
This statement returns one row for each distinct department ID. Each row contains the department ID and the sum of the salaries of the employees in the department.

Notes

The driver uses the MongoDB aggregation framework whenever possible. In some instances, MongoDB may aggregate data in unexpected ways. For example, if you use the GROUP BY clause to return zip codes and the database contains the zip code 15237 as both a string and an integer, then two rows will be returned for 15237 (one for the string representation and another for the integer representation).