Sql Server Group By Clause – javatpoint
Mục Lục
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”:
Following is a list of some inserted data in the table.
GROUP By using SUM Function
See this example where we GROUP BY department from the “Employee2” using SUM function:
Output:
GROUP By using COUNT Function
See this example where we GROUP BY designation from the “Employee2” using COUNT function:
Output:
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:
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:
Next Topic
SQL Server WHERE Clause