SQL SUM() AND AVG() Functions

SQL AVG and SQL SUM

Understanding SQL SUM() & AVF Function

In the realm of SQL (Structured Query Language), aggregation functions play a vital role in manipulating and analyzing data.Among these functions, two commonly used ones are SUM() and AVG(). They enable us to perform calculations on a set of values and extract valuable insights from our database tables.

This article will delve into the concepts and applications of SQL’s SUM() and AVG() functions in depth.

Understanding the SQL SUM() Function

The SUM() function is an aggregation function in SQL that calculates the sum of all values in a column. It is commonly used with numeric data types such as integers, decimals, or floating-point numbers.

By applying the SUM() function, we can quickly obtain the total value of a particular column, which is especially useful when working with financial data, sales figures, or any scenario that requires adding up values.

SELECT SUM(column_name)
FROM table_name
WHERE condition;

Here, column_name refers to the specific column from which we want to calculate the sum, and table_name represents the name of the table that contains the column. Additionally, the optional WHERE clause allows us to specify conditions to filter the data before applying the sum calculation.

SQL SUM Function
  1. SELECT SUM(amount) AS total_sales: This line specifies the selection of a sum of values from the “amount” column in the “Orders” table. The result of this sum will be referred to as “total_sales”.
  2. FROM Orders: This line indicates that the data should be retrieved from the “Orders” table.
  3. total_sales: This is the alias given to the result of the sum operation. It allows us to refer to the calculated sum as “total_sales” in the output.
  4. The final result of the query is the sum of all the values in the “amount” column of the “Orders” table, which is 12,099. This represents the total sales amount across all orders in the table.
SQL SUM() function with WHERE clause

The query is written in SQL and retrieves specific information from the “Orders” table. Let’s break it down:

  1. SELECT SUM(amount) AS total_of_cus3: This line selects the sum of the “amount” column from the “Orders” table. The result of this sum will be referred to as “total_of_cus3” in the output.

  2. FROM Orders: This line specifies that the data should be retrieved from the “Orders” table.

  3. WHERE Customer_id = 3: This line adds a condition to the query. It specifies that only rows where the “Customer_id” column is equal to 3 should be considered.

The final result of the query is the sum of the “amount” values for all rows where the “Customer_id” is 3. In this case, the sum is 1400, indicating the total amount spent by the customer with the ID 3.

Understanding the SQL AVG() Function

The SQL AVG() function is an aggregate function used to calculate the average value of a set of numeric data within a database table. It operates on a column or an expression that represents numerical values and returns the average as a result.

SELECT AVG(column_name) FROM table_name;
SQL AVG age

To calculate the average age of the customers in the given table, we use the SQL query:

SELECT AVG(age) AS average_age
FROM customers;

This query selects the ‘age’ column from the ‘customers’ table and applies the AVG function to calculate the average value. The result is then assigned the alias ‘average_age’ for clarity.

In the provided table, the ages of the customers are 30, 22, 21, 25, and 28. By taking the average of these values, we get 25.2. Therefore, the average age of the customers in the table is 25.2 years.

SQL AVG() function with GROUP BY clause

The provided query is written in SQL and retrieves specific information from the “Orders” table. Let’s break it down:

  1. SELECT Customer_id, AVG(amount) AS average_spends: This line selects two columns: “Customer_id” and the average of the “amount” column. The AVG function is used to calculate the average value. The alias “average_spends” is given to the calculated average in the output.

  2. FROM Orders: This line specifies that the data should be retrieved from the “Orders” table.

  3. GROUP BY Customer_id: This line groups the rows in the table based on the “Customer_id” column.

  4. The final result of the query is the average amount spent by each customer. It groups the rows based on the “Customer_id” and calculates the average “amount” for each group.
  5. In this case, the result shows three rows: customer 1 has an average spend of 350, customer 2 has an average spend of 9999, and customer 3 has an average spend of 700.
  6. These values represent the average amount spent by each customer based on their respective orders in the table.

Question 1. What happens if there are NULL values in the column?

If the column contains NULL values, the SQL SUM() function will treat them as zeros (0) and include them in the calculation. Therefore, the result of the SUM() function may include the sum of non-NULL values as well as the sum of NULL values treated as zeros.

Question 2. Can I use SUM() or AVG() with non-numeric data?

No, the SQL SUM() and AVG() functions are designed to work with numeric data types only. Attempting to use them with non-numeric data will result in an error. If you need to perform calculations on non-numeric data, you may need to consider other functions or techniques specific to the data type.

Question 3. Are there any limitations to using SUM() and AVG()?

While the SUM() and AVG() functions are powerful for calculating sums and averages, there are some limitations to keep in mind. These functions can only be applied to columns or expressions that have a numeric data type. Additionally, when using the SUM() function, the result may overflow if the sum exceeds the maximum value that can be stored in the data type.

Question 4. Can I use SUM() and AVG() together in the same query?

Yes, you can use both the SUM() and AVG() functions in the same query. These functions serve different purposes, with SUM() calculating the total sum and AVG() calculating the average. Depending on your analysis requirements, you may need to use one or both of these functions in combination to gain insights into your data.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription