MySQL Sum Function
MySQL SUM() function returns the total sum of a numeric column.
Demo Database:
- Table Name : products
order_id | order_no | order_date | Customer Id | total_amount | status |
---|---|---|---|---|---|
1 | 2021001 | 2021-05-13 | 1 | 1545.00 | Delivered |
2 | 2021002 | 2021-05-13 | 2 | 18000.00 | Delivered |
3 | 2021003 | 2021-05-14 | 3 | 10000.00 | Pending |
4 | 2021004 | 2021-05-15 | 4 | 1450.00 | Delivered |
5 | 2021005 | 2021-05-16 | 5 | 4680.00 | Pending |
6 | 2021006 | 2021-05-17 | 6 | 10000.00 | Pending |
7 | 2021007 | 2021-05-18 | 7 | 5475.00 | Delivered |
8 | 2021008 | 2021-05-19 | 8 | 4337.00 | Pending |
9 | 2021009 | 2021-05-20 | 9 | 47500.00 | Delivered |
10 | 2021010 | 2021-05-21 | 10 | 44345.00 | Delivered |
1.Sum() function
Example
Return the sum of all price fields in the products table.
select sum(total_amount) from orders;Try it Yourself
2.Sum() function with where clause
Example
Return the sum of all price fields in the products where unit='nos'.
select sum(total_amount) from orders where status='Delivered';Try it Yourself
3.Sum() function with GROUP BY clause
Example
Here we use the sum with group by clause that returns the sum of all price in the products table.
select sum(total_amount),order_date from orders group by order_date;Try it Yourself