SQL GROUP BY

http‮ww//:s‬w.theitroad.com

SQL GROUP BY is a clause used to group rows in a result set based on one or more columns. It is commonly used with aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() to perform calculations on groups of rows.

The syntax for GROUP BY is as follows:

SELECT column1, column2, ... FROM table_name WHERE condition GROUP BY column_name1, column_name2, ...;

Here, column1, column2, and so on represent the columns you want to retrieve from the table, table_name is the name of the table you want to retrieve data from, condition is an optional clause that specifies conditions for filtering data, and column_name1, column_name2, and so on represent the columns you want to group the result set by.

For example, the following statement retrieves the number of employees in each department from the "employees" table:

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

This statement groups the result set by the "department" column and counts the number of employees in each group.

You can group the result set by multiple columns by listing them in the GROUP BY clause separated by commas. For example:

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

This statement groups the result set by the "department" and "gender" columns and counts the number of employees in each group. This will give you a count of employees for each department and gender combination.