You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/wwl-azure/basic-sql-aggregate-functions-grouping/includes/3-aggregate-functions.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
Instead of retrieving individual rows, you frequently need to summarize data to understand trends or patterns. Aggregate functions are designed for this purpose, allowing you to perform calculations on a group of rows and return a single result. Common examples include determining the total sales, the average price of products, or the number of orders placed. These functions are fundamental tools for data analysis in SQL.
2
+
2
3
Examples of aggregate functions include `COUNT` to count rows, `SUM` to calculate totals, `AVG` for averages, `MIN` to find the smallest value, and `MAX` to find the largest value in a dataset.
3
4
4
5
## COUNT
@@ -8,11 +9,13 @@ Examples of aggregate functions include `COUNT` to count rows, `SUM` to calculat
8
9
```sql
9
10
SELECTCOUNT(*) FROMsales.orders;
10
11
```
12
+
11
13
The result of this query is a single numeric value representing the total number of orders (or rows) present in the `sales.orders` table.
This SQL query uses the `COUNT` function along with the `DISTINCT` keyword to determine the number of unique countries present in the `country` column of the `sales.customers` table. Essentially, it counts each distinct `country` only once, providing a numerical result that represents the total number of different countries in the dataset.
17
20
18
21
## SUM
@@ -22,6 +25,7 @@ This SQL query uses the `COUNT` function along with the `DISTINCT` keyword to de
22
25
```sql
23
26
SELECTSUM(price) FROMsales.products;
24
27
```
28
+
25
29
This query calculates the total sum of all values in the `price` column of the `sales.products` table. It aggregates the prices of multiple rows into a single numeric result, representing the total cost of all products.
26
30
27
31
## AVG
@@ -31,6 +35,7 @@ The `AVG` function is an essential SQL aggregate function designed to calculate
31
35
```sql
32
36
SELECTAVG(price) FROMsales.products;
33
37
```
38
+
34
39
This query is designed to calculate the average `price` of all products listed in the `sales.products` table. By using the `AVG` function, it computes the arithmetic mean of the values in the `price` column, effectively summarizing the dataset into a single representative value.
35
40
36
41
## MAX and MIN
@@ -40,4 +45,5 @@ The `MAX` and `MIN` functions are aggregate functions used to identify the highe
40
45
```sql
41
46
SELECTMAX(price), MIN(price) FROMsales.products;
42
47
```
48
+
43
49
This query retrieves two values: the highest `price` and the lowest `price` among all products listed in the `sales.products` table.
Copy file name to clipboardExpand all lines: learn-pr/wwl-azure/basic-sql-aggregate-functions-grouping/includes/4-group-by-having.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
To gain deeper insights and better organize data within a database, SQL introduces the powerful `GROUP BY` and `HAVING` clauses.
1
+
To gain deeper insights and better organize data within a database, SQL introduces the powerful `GROUP BY` and `HAVING` clauses.
2
+
2
3
These tools allow you to transform raw data into meaningful summaries by categorizing rows and applying aggregate functions, making it easier to analyze trends, identify patterns, or generate reports tailored to specific categories.
3
4
4
5
## GROUP BY
@@ -17,6 +18,7 @@ SELECT category_id, AVG(price)
17
18
FROMsales.products
18
19
GROUP BY category_id;
19
20
```
21
+
20
22
This query retrieves the average price of products within each category by grouping the rows of the `sales.products` table according to their `category_id`. By applying the `AVG()` function to the `price` column, the query calculates the mean value of prices for all products under every distinct `category_id`.
21
23
22
24
`GROUP BY` clause doesn't always need to be paired with aggregate functions. While it's commonly used alongside aggregate functions to perform calculations on grouped data, `GROUP BY` can also be used by itself to organize data into groups for other purposes, such as retrieving distinct combinations of columns.
@@ -26,6 +28,7 @@ SELECT category_id
26
28
FROMsales.products
27
29
GROUP BY category_id;
28
30
```
31
+
29
32
This query groups the rows by `category_id` without performing any aggregation, simply listing each distinct `category_id` present in the `sales.products` table.
30
33
31
34
## HAVING
@@ -40,4 +43,5 @@ FROM sales.products
40
43
GROUP BY category_id
41
44
HAVINGAVG(price) >30;
42
45
```
46
+
43
47
This query calculates the average `price` of products within each category by grouping rows of the `sales.products` table according to their `category_id`. It then filters the results to include only those categories where the average `price` exceeds 30 using the `HAVING` clause.
0 commit comments