SQL -Structured Query Language - Sql Select - Select - Sum Tutorial
To retrieve sum of the records of a numeric column from the existing created table. Then we can use a ' SELECT ' statement with the SUM() function.
Without condition
Syntax-
The basic syntax of the SELECT statement with SUM() function is given below –
SELECT SUM(numeric_column_name) FROM table_name;
For example-
- Using numeric column name
SELECT SUM(salary) FROM employees;
Here it will retrieve a sum of salary in the record from the employees' table.
We can use whereas Condition / Clause in a select statement such as WHERE, GROUP BY, HAVING, ORDER BY, ETC.
With condition (USING WHERE)
Syntax-
The basic syntax of the ‘ SELECT ‘ statement with SUM() function using WHERE condition is given below –
SELECT SUM(numeric_column_name) FROM table_name WHERE [CONDITION] ;
For example
- Using numeric column name
SELECT SUM(salary) FROM employees WHERE employee_id>150;
Here it will retrieve a sum of salary from the employee table whose employee_id is greater than 150.