## Practical 7: Aggregrate Functions

2024, May 8

2024, May 8

Aggregate functions are essential for summarizing data in SQL. They operate on a set of values and return a single value. Here, we cover key aggregate functions:

**MIN**: Returns the smallest value in a given column.**MAX**: Returns the largest value in a given column.**SUM**: Returns the sum of the numeric values in a given column.**AVG**: Returns the average value of a given column.**COUNT**: Returns the total number of values in a given column.**COUNT(*)**: Returns the number of rows in a table.

Aggregate functions compute results based on a column of numeric data returned by a `SELECT`

statement. They summarize the results of a selected data column. These functions are integral to the `GROUP BY`

clause but can also be used independently.

The following examples demonstrate how to use aggregate functions in SQL:

`SELECT AVG(salary) FROM employee;`

This query returns the average salary of all employees.

```
SELECT AVG(salary)
FROM employee
WHERE title = 'Programmer';
```

This query returns the average salary for employees with the title 'Programmer'.

`SELECT COUNT(*) FROM employees;`

This query returns the number of rows in the `employees`

table.

- Select the maximum price of any item ordered in the
`items_ordered`

table. - Select the average price of all items ordered in December.
- Count the total number of rows in the
`items_ordered`

table. - Find the lowest price of tents ordered in the
`items_ordered`

table.

`SELECT MAX(price) FROM items_ordered;`

```
SELECT AVG(price)
FROM items_ordered
WHERE order_date LIKE '%Dec%';
```

`SELECT COUNT(*) FROM items_ordered;`

```
SELECT MIN(price)
FROM items_ordered
WHERE item = 'Tent';
```