How GROUP BY internaly works in SQL?
Hi there, I am going to explain a summary about sentence in the above in this article. So without any further ado let’s get started.
GROUP BY clause often times confusing any developer, who want to start to learning SQL when he encounter. To be honestly I was confusing as well when I start to learning SQL GROUP BY clause. That’s why I will explain it to be clearly.
As you know GROUP BY clause groups each row of the same values, when you read article or watch videos in a web platform, but what exactly that? To understand GROUP BY clause you should visualization it when you work with GROUP BY clause in SQL. I was explain it like visualization in the following picture. For an instance we have a table which is called foods.
and we want to know that how many each of food have in the above table to query from this table will be looks like in the following picture.
when you query you encounter data like in the following picture.
Let’s explain step by step how internally GROUP BY works? But before passing explain I want to introduce you the visualization picture in the following.
- As you see SQL prepare extra column given by GROUP BY clause of column_name firstly.
- After that corresponding to this clause of column of value of each records are combined.
That’s it I hope you understand how exaclty GROUP BY works and when you finish this article you can easily understand every GROUP BY clause written by SQL. Keep in mind SQL without visualization impossible.