When should we use GROUP BY clause

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’

One Response to When should we use GROUP BY clause

  1. Pingback: Difference between ‘distinct’ and ‘group by’ clauses | java tech stack

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey

%d bloggers like this: