Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL GROUP BY Statement #1808

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/languages/SQL/sql-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
id: sql-group-by
sidebar_position: 21
title: "SQL GROUP BY Statement"
sidebar_label: "SQL GROUP BY"
description: "The SQL GROUP BY statement is used to group rows that have the same values in specified columns and apply aggregate functions."
tags: [sql, dbms, database, group by]
---

The `GROUP BY` statement in SQL is used to arrange identical data into groups. It is typically used with aggregate functions (such as `COUNT`, `SUM`, `AVG`, `MAX`, or `MIN`) to perform operations on each group.

### Syntax

```sql
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
```

### Key Points

* The GROUP BY statement groups rows that have the same values in specified columns.
* It is often combined with aggregate functions to perform calculations on each group.
* Using ORDER BY with GROUP BY allows you to sort the grouped results.

### Examples

**Example 1: Count the number of products in each category**

```sql
SELECT CategoryID, COUNT(ProductID) AS ProductCount
FROM Products
GROUP BY CategoryID;
```

**Example 2: Find the total sales for each salesperson**

```sql
SELECT SalespersonID, SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY SalespersonID;
```

**Example 3: Retrieve the average salary of employees in each department**

```sql
SELECT DepartmentID, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY DepartmentID;
```

**Note:**
* The GROUP BY clause must appear after the WHERE clause and before the ORDER BY clause if they are used together.
* When using GROUP BY, only the grouped columns or aggregate functions can be included in the SELECT statement, as other columns would produce ambiguous results.
* The HAVING clause can be used with GROUP BY to filter groups based on aggregate conditions.
Loading