When should we use GROUP BY clause
October 9, 2012 1 Comment
When there is a need to aggregate/group values in a column based on some criteria in same/another column then we go for GROUP BY clause.
Usage of Aggregate function on column: col_sales:
select sum(col_sales) from Table_Sales;
Output: here the output would just one value
Usage of Aggregate function on column ‘col_sales’ based on date in column ‘col_sales_date’
Problem Statement: Calculate the total sales on day by day basis (OR daily sales) from Table_Sales table
Query:
select col_sales_date, sum(col_sales) from Table_Sales GROUP BY col_sales_date;
Output: here the output would be number of rows proportional to the number of different dates in the column ‘col_sales_date’
Pingback: Difference between ‘distinct’ and ‘group by’ clauses | java tech stack