Javatpoint Logo
Javatpoint Logo

SQL Server GROUP BY Clause

SQL Server GROUP BY clause is used to collect data across multiple records and group the results by one or more columns. It is used with SELECT statement.

Syntax:

Parameter explanation

expression1, expression2, ... expression_n: These expressions are not encapsulated within an aggregate function and must be included in the GROUP BY clause.

aggregate_function: It can be a function such as SUM, COUNT, MIN, MAX, or AVG functions.

tables: The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions: It is optional. The conditions that must be met for the records to be selected.

Example:

First create a table "Employee2":

SQL server group by clause 1

Following is a list of some inserted data in the table.

SQL server group by clause 2

GROUP By using SUM Function

See this example where we GROUP BY department from the "Employee2" using SUM function:

Output:

SQL server group by clause 3

GROUP By using COUNT Function

See this example where we GROUP BY designation from the "Employee2" using COUNT function:

Output:

SQL server group by clause 4

GROUP By using MIN Function

See this example where we GROUP BY department on the basis of salary from the "Employee2" using MIN function.

This will retrieve the minimum salary according to department:

Output:

SQL server group by clause 5

GROUP By using MAX Function

See this example where we GROUP BY department on the basis of salary from the "Employee2" sing MAX function.

This will retrieve the maximum salary according to department:

Output:

SQL server group by clause 6
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA