Skip to content

Commit 01d64e1

Browse files
authored
Merge pull request #332 from khushi-joshi-05/patch-1
Create grouping-data.md
2 parents badfd8a + a5802f2 commit 01d64e1

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Grouping Data in SQL
2+
3+
When you work with a lot of data, you often want to **combine rows that have the same values** in certain columns and calculate something for each group.
4+
In SQL, this is done with the **`GROUP BY`** clause.
5+
6+
7+
8+
## Why Use GROUP BY?
9+
10+
Imagine you have a `sales` table:
11+
12+
| id | product | category | quantity | price |
13+
|----|-----------|-----------|----------|-------|
14+
| 1 | Apple | Fruit | 10 | 2.5 |
15+
| 2 | Orange | Fruit | 5 | 3.0 |
16+
| 3 | Carrot | Vegetable | 7 | 1.5 |
17+
| 4 | Apple | Fruit | 8 | 2.5 |
18+
19+
If you want **total quantity sold for each category**, you can group the rows by `category`.
20+
21+
22+
23+
## GROUP BY Syntax
24+
25+
```sql
26+
SELECT column1, column2, ..., aggregate_function(column)
27+
FROM table_name
28+
GROUP BY column1, column2, ...;
29+
````
30+
31+
* **`aggregate_function`**: Functions that calculate a value for a group, such as:
32+
33+
* `COUNT()` → Counts rows
34+
* `SUM()` → Adds values
35+
* `AVG()` → Calculates average
36+
* `MIN()` → Finds smallest value
37+
* `MAX()` → Finds largest value
38+
39+
40+
## Example: GROUP BY with SUM
41+
42+
```sql
43+
SELECT category, SUM(quantity) AS total_quantity
44+
FROM sales
45+
GROUP BY category;
46+
```
47+
48+
**Result:**
49+
50+
| category | total\_quantity |
51+
| --------- | --------------- |
52+
| Fruit | 23 |
53+
| Vegetable | 7 |
54+
55+
**How it works:**
56+
57+
1. SQL looks at the `category` column.
58+
2. Rows with the same category are grouped together.
59+
3. The `SUM(quantity)` is calculated for each group.
60+
61+
62+
63+
## GROUP BY with Multiple Columns
64+
65+
You can group by **more than one column**.
66+
67+
```sql
68+
SELECT category, product, SUM(quantity) AS total_quantity
69+
FROM sales
70+
GROUP BY category, product;
71+
```
72+
73+
Now each unique **(category, product)** pair is its own group.
74+
75+
76+
77+
## Filtering Groups with HAVING
78+
79+
`WHERE` filters **rows before grouping**.
80+
`HAVING` filters **groups after grouping**.
81+
82+
Example: Show only categories where total quantity > 10.
83+
84+
```sql
85+
SELECT category, SUM(quantity) AS total_quantity
86+
FROM sales
87+
GROUP BY category
88+
HAVING SUM(quantity) > 10;
89+
```
90+
91+
**Result:**
92+
93+
| category | total\_quantity |
94+
| -------- | --------------- |
95+
| Fruit | 23 |
96+
97+
98+
99+
## Difference Between WHERE and HAVING
100+
101+
| Clause | Filters On | Works With Aggregates? |
102+
| ------ | --------------- | ---------------------- |
103+
| WHERE | Individual rows | ❌ (no aggregates) |
104+
| HAVING | Grouped results | ✅ (with aggregates) |
105+
106+
107+
108+
## Common Aggregate Functions
109+
110+
| Function | Description | Example |
111+
| ----------- | ----------------- | --------------- |
112+
| COUNT(\*) | Counts all rows | `COUNT(*)` |
113+
| SUM(column) | Adds all values | `SUM(quantity)` |
114+
| AVG(column) | Average of values | `AVG(price)` |
115+
| MIN(column) | Minimum value | `MIN(price)` |
116+
| MAX(column) | Maximum value | `MAX(price)` |
117+

0 commit comments

Comments
 (0)