The SQL Group by statement is used to group the results of a SELECT query based on one or more columns.
The syntax for using Group by in a statement.
- SELECT column_name, function(column_name)
- FROM table_name
- WHERE condition
- GROUP BY column_name
Example of Group by in a Statement
Consider the following Consumer table.
Here we will sort or I mean grouped by their salaries
SQL query for the above requirement will be,
- SELECT name, age
- from Consumers group by salary
Result will be,
Example of Group by in a Statement with WHERE clause
Consider the following Emp table
SQL query will be,
- select name, salary
- from Consumer
- where age > 28
- group by salary
The result will be.