Sobes.tech
Junior — Middle

Explain the purpose of the GROUP BY operator in SQL and when it is used.

sobes.tech AI

Answer from AI

The GROUP BY operator in SQL is used to group rows of a table by one or more columns. This allows aggregating data, such as calculating the sum, average, or count of elements in each group.

It is used when you need to obtain summary information, for example, the number of orders per customer or the average income by region.

Example:

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;

In this query, we group employees by departments and count the number of employees in each department.