SELECT sum( column_name ) FROM table_name
The command added all the values of the mark field and display.
SELECT SUM( mark ) FROM `student`
| SUM(mark) |
| 390 |
Let us learn about the SUM() SQL command and how to use it in our tables. SUM
command can be applied to numeric field and the total of the value is returned.
Now let us apply this SUM command to this table and find out the total mark
obtain by all the students. The SUM command will add all the values of the mark
field and return to us. This is our table | id | name | class | mark |
| 1 | John Deo | Four | 75 |
| 2 | Max Ruin | Three | 85 |
| 3 | Arnold | Three | 55 |
| 4 | Krish Star | Four | 60 |
| 5 | John Mike | Four | 60 |
| 6 | Alex John | Four | 55 |
SELECT SUM(mark) as total_mark FROM `student`
| total_mark |
| 390 |
SELECT class, sum( mark ) as total_mark FROM`student` GROUP BY class
| class | total_mark |
| Four | 250 |
| Three | 140 |
SELECT SUM( mark ) as total_mark, class FROM student WHERE mark > 55 GROUP BY class
| total_mark | class |
| 195 | Four |
| 85 | Three |
Other sql commands like between can be used along with this sum command to
find out required results.
SELECT sum( mark ) FROM `student` where id IN(1,2,3,4)
SELECT SUM(sell_price - buy_price) as Profit FROM plus2_product
Output here is 5 as Profit. SELECT SUM(sell_price - buy_price) as Profit FROM plus2_product WHERE product ='Product1'
SELECT product,buy_price,sell_price FROM plus2_product
UNION
SELECT 'Total:',SUM(buy_price),SUM(sell_price) FROM plus2_product
| product | buy_price | sell_price |
|---|---|---|
| Product1 | 20 | 22 |
| Product2 | 15 | 20 |
| Product3 | 40 | 43 |
| Product1 | 50 | 45 |
| Total: | 125 | 130 |
CREATE TABLE IF NOT EXISTS `plus2_product` (
`product` varchar(20) NOT NULL,
`buy_price` int(3) NOT NULL,
`sell_price` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `plus2_product`
--
INSERT INTO `plus2_product` (`product`, `buy_price`, `sell_price`) VALUES
('Product1', 20, 22),
('Product2', 15, 20),
('Product3', 40, 43),
('Product1', 50, 45);
Find out non-numeric values present in a Column by using CONCAT
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
| zin el ouni | 16-09-2010 |
| how Ican execute Querie union (2 table with many field and one Field numéric), with this field i want make an Operator sum | |
| JTM | 11-08-2011 |
| Hi! can someone tell me how to sum two or more rows from one table but different categories?I have a table that summarises my sales per category but i want to generate a report that summarizes all the items sold per category and return it in one single row.Please help | |
| ketan | 15-01-2012 |
| how to lisi a record from second to fifth from mysql db in php??? | |
| Rajesh | 13-02-2012 |
| Excellent Tutorial. | |
| Habib Ullah | 27-08-2013 |
| Best Tutorial | |
| julius pogi | 24-09-2013 |
| SELECT ACCOUNTTITLE,sum(debit),sum(credit) FROM `FIXEDJOURNALDTL` GROUP BY ACCOUNTTITLE | |
| amar | 28-09-2013 |
| after getting the sum of column value in grid view .i want to display in text box.pls guard me... | |
| Vivek | 23-09-2015 |
| Thank you for your tutorial.But I need id 1 to 4 sum values only, if any options is there? please tel... | |
| smo | 26-09-2015 |
| Added this part by using SQL IN | |
| harosa | 17-01-2016 |
| thank you for an excellent explanation! | |
| shellu | 08-10-2016 |
| excellent | |
| Aziz | 10-02-2019 |
| how to get Sum after Round a field in Group Clause Select Sum (Round (Field ,2)) from Tabel Gruop By..... | |
| smo1234 | 11-02-2019 |
| Use round() function | |
06-02-2021 | |
| Thanks Bro U R So Good Thank U Soo Much | |
17-02-2021 | |
| I have selected and listed ProductId,ProductName,Price SELECT ProductId,ProductName,Price FROM Products WHERE Price >= 50.00 Now how do I get a total of this while still retaining the listed items Thanks in Advance John R | |
18-02-2021 | |
| Use over() with Partition. | |