SQL SUM
SQL SUM
The SQL SUM function returns the sum value of all rows in a select query.
SUM syntax
SELECT SUM(table_column) FROM table_name;
SUM example
Coder books table
| ID | Title | Price | Description |
|---|---|---|---|
| 1 | Learn SQL | 20 | Learn SQL language |
| 2 | Learn MySQL | 22 | Learn MySQL language |
| 3 | HTML book | 17 | Learn HTML |
| 4 | Learn PHP | 20 | Introduction to PHP |
| 5 | Learn PHP | 20 | PHP course |
SELECT SUM(price) FROM coder_books;
Result
99
SELECT price, SUM(price) sum_price FROM coder_books GROUP BY price HAVING count(*) > 1;
Result
| Price | sum_price |
|---|---|
| 20 | 60 |