Oracle SQL GROUP BY Clause Tutorial
In Oracle database, the GROUP BY clause is used to group the query results based on specified columns. The specific usage is as follows:
SELECT column1, column2, aggregate_function(column3)
FROM table_name
GROUP BY column1, column2;
In the given statement, column1 and column2 are the columns that need to be grouped, while aggregate_function is the function used to aggregate the results after grouping, such as SUM, AVG, COUNT, etc. It is important to note that, besides aggregate functions, the columns in the SELECT statement must either be in the GROUP BY clause or be parameters of the aggregate function.
For example, if we have a table called ‘students’ that includes students’ names, ages, and grades, we can use the following statement to group students by age and calculate the average grade for each age group:
SELECT age, AVG(grade)
FROM students
GROUP BY age;
This will result in the average score for each age group. It is important to note that if you want to group all columns, you can simply use the GROUP BY clause without specifying any columns, which will merge all rows into one group.