Sobes.tech
Junior — Middle

Can you explain the purpose of the GROUP BY operator in SQL and when to use it?

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 for aggregating data, such as calculating the sum, average, or count of records in each group.

Example of usage: if you have a sales table and want to find the total sales for each salesperson, you group the data by the salesperson's identifier and apply the aggregate function SUM to the sales amount column.

SELECT seller_id, SUM(sale_amount) AS total_sales
FROM sales
GROUP BY seller_id;

Use GROUP BY when you need to obtain aggregated data grouped by certain features.

Can you explain the purpose of the GROUP BY operator… - sobes.tech