x

SQL – GROUP BY()

Prev     Next

SQL GROUP BY() function is used in conjunction with a SELECT statement and aggregate functions (such as SUM(), AVG(), MAX() etc.), to group similar data in result-set by one or more columns. Syntax for SQL GROUP BY() function is given below.


SQL Syntax for GROUP BY() function:

Syntax for SQL GROUP BY() function SELECT
column_name,
aggregate_function
(column_name)
FROM table_name
WHERE [conditions] GROUP BY
column_name;
Syntax for SQL GROUP BY() + ORDER BY() function SELECT
column_name,
aggregate_function
(column_name)
FROM table_name
WHERE [conditions] GROUP BY
column_name
ORDER BY
column_name1,
column_name2;

Note: When we use both GROUP BY() and ORDER BY() functions, the above order must be followed.

Please consider the following table with few records as given below.

Table name (for example): student
Column names in this table: Student_ID, Student_name, Department, Marks, City and Age
Available records: 6 rows