Aggregate Functions

SQL (Structured Query Language) is a standard language used to manage and manipulate relational databases. Aggregate functions and indexes are important features of SQL that help to make database queries more efficient and effective.

Aggregate Functions

Aggregate functions are used to perform calculations on a set of values and return a single value. Here are some of the commonly used aggregate functions in SQL:

COUNT

The COUNT function returns the number of rows that match a specified condition. It can be used with or without a WHERE clause.

Example:

SELECT COUNT(*) FROM customers;

This will return the total number of rows in the 'customers' table.

SUM

The SUM function returns the sum of a numeric column in a table. It can be used with or without a WHERE clause.

Example:

SELECT SUM(price) FROM orders WHERE status='completed';

This will return the total sum of prices for completed orders.

AVG

The AVG function returns the average of a numeric column in a table. It can be used with or without a WHERE clause.

Example:

SELECT AVG(age) FROM employees WHERE department='sales';

This will return the average age of employees in the sales department.

MIN

The MIN function returns the minimum value of a column in a table. It can be used with or without a WHERE clause.

Example:

SELECT MIN(salary) FROM employees WHERE department='finance';

This will return the minimum salary of employees in the finance department.

MAX

The MAX function returns the maximum value of a column in a table. It can be used with or without a WHERE clause.

Example:

SELECT MAX(sales) FROM sales_data WHERE year=2022;

This will return the maximum sales value for the year 2022.

Last updated