Group by Clause and Aggregated function in SQL || Steem Alliance Community.

in hive-150122 •  7 months ago 

Hello Everyone,


Red Abstract Income Money YouTube Thumbnail_20240214_130840_0000.png

How are you, i hope you guys will happy and well. By thre grace of God, i am also well. Today i am here with new post of SQL and PL/SQL series in which i am going to discuss about Distinct key word and order by clause.

Group by clause is basically used with aggregate functions. first of all, we will discuss about aggregate function. then we will move on group by clause. there are many types of aggregate functions that are used in our daily routine development in sql.

Aggregated functions:

  • Sum()
  • Avg()
  • min()
  • max()
  • count()

First of all we i will share th output of a table b using simple select statement.

NOsectorcust_idname_1Town
110004270603SIKANDARPESHAWAR
210004270939TARIQALI PUR
310005715170ALI HAMZAGALA YOUSAF
410007796882IMRANKHANEWAL
51000953045UMARYAZMAN
61000953045UMARYAZMAN

If we want to count the total rows of tables then we will use the count() function. below is the query for table data counting.

select count(cust_id) from users



Result of query.

count(cust_id)
6

To calculate the total value of a particular column within a table, the SUM() function will use. Below is the query for aggregating table data using this function.

select sum(cust_id) from users



Result of query.

sum(cust_id)
23959684

To find the minimum values from a particular column within a table then the min() function will use. Below is the query for aggregating table data using this function.

select min(cust_id) from users



Result of query.

min(cust_id)
953045

images (8).png
Img SRC

I hope things will clear now we move on group by function. group by clause is used to get the result of data from table on group based on depending on the values in one or more columns.

select sum(cust_id) , sector  from users group by sector



Result of query.

sector of each row is same so it will sum the cust_id on the base of sector. same sector will group by and then sum function will apply on it and then show the result.

sum(cust_id)sector
239596841000

Now we will apply the group by function on the base of two colums then see the result.

select sum(cust_id) , sector , name_1 from users group by sector , name_1

Result of query:

NOsectorcust_idname_1
110004270603SIKANDAR
210004270939TARIQ
310005715170ALI HAMZA
410007796882IMRAN
510001906090UMAR

In the preceding outcome, I utilized the sum() function with the group by clause to aggregate the cust_id values. The group by function was applied based on the sector and name_1 columns. While the sector remains same across all rows, the name_1 values differ. the data 'Umar' in name_1 appears twice with the same sector, so the group by function will add up these two lines.

It would be my pleasure if you will share your opinion and reviews on this topic

20230310_190323_0000.png

Please cast witness vote to @bangla.Witness or set proxy to @rme.

_
Vote @bangla.witness as witness
Set Proxy @rme as proxy

Special Thanks.

Cc: @rme
Cc: @hungry-griffin
Cc: @steemcurator02

Thanks for giving your precious time to my post.
Follow | Upvote | Share | Comments
Follow me on Instagram.
Instagram

🆃🅷🅰️🅽🅺 🆈🅾️🆄

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.