The GROUP BY
clause is a powerful SQL feature that allows users to summarize data by specific categories before applying aggregate functions. By grouping rows that share common attributes, it enables more detailed analysis. The syntax for using GROUP BY
is straightforward: SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name;
. For example, to calculate the average salary per department, you would write:
sqlCopy codeSELECT department_id, AVG(salary) FROM employees GROUP BY department_id;
This query will return the average salary for each department, providing insights into compensation structures. Using GROUP BY
enhances the analytical capabilities of SQL queries, allowing for more nuanced understanding of the data.
Leave a Reply