SQL SUM() and AVG()
SQL SUM() and AVG() are aggregate functions that are used to perform calculations on a set of values in a column.
The SUM() function is used to calculate the sum of all values in a column. The syntax is:
refer :ottheitroad.comSELECT SUM(column_name) FROM table_name WHERE condition;
Here, column_name
is the name of the column you want to calculate the sum for, table_name
is the name of the table where the column is located, and condition
is an optional condition that specifies which rows to include in the calculation.
The AVG() function is used to calculate the average of all values in a column. The syntax is:
SELECT AVG(column_name) FROM table_name WHERE condition;
Here, column_name
is the name of the column you want to calculate the average for, table_name
is the name of the table where the column is located, and condition
is an optional condition that specifies which rows to include in the calculation.
Both the SUM() and AVG() functions can be used with numeric columns, such as those that contain integers, decimals, or other numeric values. When used with non-numeric columns, the function will return an error.