Junior — Middle
What is the purpose of the GROUP BY clause in SQL and how is it used for data processing?
sobes.tech AI
Answer from AI
The GROUP BY instruction in SQL is used to group rows of a table based on the values of one or more columns. This allows for data aggregation, such as calculating sums, averages, or counting records within each group.
Example of usage:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
In this query, data is grouped by departments, and the number of employees is counted for each department.
GROUP BY is often used together with aggregate functions: COUNT(), SUM(), AVG(), MAX(), MIN().
Thus, GROUP BY helps in processing and analyzing data by highlighting statistics across categories.