SQL Tutorial
The GROUP BY
statement in SQL is used in collaboration with the aggregate functions like COUNT()
, MAX()
, MIN()
, SUM()
, AVG()
, etc. to group the result set by one or more columns.
The basic syntax of the GROUP BY
statement is:
SELECT column1, column2, ..., aggregate_function(column_name) FROM table_name WHERE condition GROUP BY column1, column2, ...;
Example:
Consider a Sales
table with the following data:
OrderID | Product | Quantity |
---|---|---|
1 | Apples | 10 |
2 | Bananas | 20 |
3 | Apples | 15 |
4 | Oranges | 12 |
5 | Apples | 20 |
6 | Bananas | 10 |
If you want to find the total quantity ordered for each product, you would use the GROUP BY
statement as follows:
SELECT Product, SUM(Quantity) FROM Sales GROUP BY Product;
This query would return:
Product | SUM(Quantity) |
---|---|
Apples | 45 |
Bananas | 30 |
Oranges | 12 |
This shows the total quantity for each product.
Note:
The GROUP BY
statement is often used with aggregate functions (like COUNT
, SUM
, AVG
, MAX
, MIN
) to group the result-set by one or more columns.
Columns listed in the GROUP BY
clause should be either used in aggregate function or should be included in the SELECT
clause.
As always, the exact syntax may vary between different SQL dialects, so be sure to consult the documentation for the SQL dialect you're using.
Grouping Data with GROUP BY in SQL:
SELECT Column1, COUNT(*) FROM TableName GROUP BY Column1;
Aggregating Functions with GROUP BY:
SELECT Department, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department;
SQL GROUP BY and HAVING Clause:
SELECT Department, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department HAVING AVG(Salary) > 50000;
GROUP BY vs DISTINCT in SQL:
SELECT DISTINCT Column1 FROM TableName;
Multiple Columns in GROUP BY Statement:
SELECT Department, Gender, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department, Gender;
Sorting Grouped Data with ORDER BY:
SELECT Department, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department ORDER BY Department;
Filtering Grouped Data with HAVING:
SELECT Department, AVG(Salary) AS AvgSalary FROM Employees GROUP BY Department HAVING AVG(Salary) > 50000;
Grouping by Expressions in SQL:
SELECT YEAR(OrderDate) AS OrderYear, COUNT(*) AS OrderCount FROM Orders GROUP BY YEAR(OrderDate);
Handling NULL Values with GROUP BY:
SELECT Column1, COUNT(*) FROM TableName GROUP BY Column1;