Last Updated: 29 June, 2023
The SUM() function in MySQL is used to calculate the sum of values in a column or expression. It is commonly used with numerical data types such as integers, floats, and decimals.
The basic syntax for the SUM() function is as follows:
SELECT SUM(column_name) FROM table_name;
Here's a breakdown of the syntax:
For example, let's say we have a table called orders with a column total_amount, and we want to calculate the sum of all the total amounts in that column. The query would look like this:
SELECT SUM(total_amount) FROM orders;
This will return a single row with the sum of all the values in the total_amount column.
We can also use the SUM() function with the GROUP BY clause to calculate the sum for each group of values in a specific column. Here's an example:
SELECT category, SUM(price) FROM products GROUP BY category;
In this example, we have a products table with columns category and price. The query will calculate the sum of prices for each category and return the category name along with the corresponding sum.
That's the basic usage of the SUM() function in MySQL. It allows us to calculate the sum of values in a column or within groups based on our requirements.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com