13/09/2021
Do you know what exactly GROUP BY in SQL does? Today you will! 😜🤘
- you should use GROUP BY if you use aggregations like SUM(), COUNT(), AVG(), MIN(), MAX().
- it groups all rows with the same values. E.g., if I have a column with values: RUN, RUN, LIFT, PUSH, PUSH, PUSH, GROUP BY groups those and gives a result of unique values: RUN, LIFT, PUSH.
An example how it looks like in SQL can be found in the pictures above.
❗Note, that
⚠ if you use aggregations and don't use GROUP BY, not everywhere you'll get an error even though you should. So be careful.
⚠ if you don't use GROUP BY, you will get repeated values. For example, if I have the same column "Terms" with values RUN, RUN, LIFT, PUSH, PUSH, PUSH, and I will write
SELECT Terms
FROM table
♦The result will be a column with all values: RUN, RUN, LIFT, PUSH, PUSH, PUSH
But with GROUP BY
SELECT Terms
FROM table
GROUP BY Terms
♦I'll get only unique (grouped) values: RUN, LIFT, PUSH.
If you like posts like this, like & save this post! 🤘 And as always, let me know your thoughts and questions below 👇