sql sum function

The SQL sum function is used to calculate the value inside a column. The SQL sum function returns the sum of the calculated value. But let you know this does not count NULL values.

Related: 25 important examples of SQL where clause

Apart from SQL sum, here are some other examples like SQL COUNT, sum, sum multiple columns, etc. after seeing which you will know how to sum in SQL.

SQL Sum Function

/ ***** Calculated the value of the salary column ***** /

SELECT SUM(salary)
FROM Employees;


/ ***** Calculated the value of the age column ***** /

SELECT SUM(age)
FROM Employees;
WHERE employeeID <=5;


/ ***** Calculated the value of the salary column ***** /

SELECT SUM(salary)
FROM Employees;
WHERE salary >=5000;


/ ***** Calculated the unique values of the salary column ***** /

SELECT SUM(DISTINCT salary)
FROM Employees;
WHERE salary <=8000;


/ ***** After calculating it will multiply ***** /

SELECT SUM(salary * 2)
FROM Employees;
WHERE salary <=4000;

SQL Sum Multiple Columns

If you want to sum multiple columns, then for that you will have to give the names of 2 columns inside the SUM function. In the following examples below, you can see how to sum two columns in SQL.

/ ***** Calculated the value of the salary and age column ***** /

SELECT SUM(age + salary) 
FROM Employees;


/ ***** Calculated the value of the salary and age column ***** /

SELECT SUM(age + salary) 
FROM Employees
WHERE ProductID <=5;


/ ***** Calculated salary and age where name is klm ***** /

SELECT SUM(salary + age) 
FROM Employees
WHERE name="klm";

SQL Count

The SQL count function is used to count total records inside any column. Let’s know how to use the SQL count function.

/ ***** It will count total names ***** /

SELECT COUNT(name)
FROM Employees;


/ ***** It will count total unique names ***** /

SELECT COUNT(DISTINCT name)
FROM Employees;


/ ***** The salary of those below 5000 will be calculated ***** /

SELECT COUNT(salary)
FROM Employees
WHERE salary <=5000;


/ ***** The age of those above 50 will be calculated ***** /

SELECT COUNT(age)
FROM Employees
WHERE age >=50;


/ ***** The name of those below 30 and salary above 4000 ***** /

SELECT COUNT(name)
FROM Employees
WHERE age <=30 AND salary >=4000;

SQL Keywords

We have some keywords of SQL available, which are used most commonly. Let’s know what those keywords are and what they mean.

NoKeywordsDescription
1CREATEIt is used to create a database and table
2CREATE DATABASEIt is used to create a database only
3CREATE TABLEIt is used to create a table
4DELETEIt is used to delete record from the table
5DROPIt is used to delete a column, database, and table
6DROP COLUMNIt is used to delete a column in the table
7DROP TABLEIt is used to delete a table
8FOREIGN KEYIt is used to join two tables together
9JOINIt is used to join the tables
10UPDATEIt is used to update the record in the table

SQL Frequently Asked Questions

We have answers to some questions that people search on the Internet every day. Let’s know what those questions are and what their answers are.

What does SQL mean?
SQL stands for Structured Query Language

How to extract data from the database?
Using the “SELECT” statement.

How to update data in the database?
Using the “UPDATE” statement.

How to delete data in the database?
Using the DELETE statement.

How to insert new data in the database?
Using the INSERT INTO statement.

how do you select a column named
Using the “SELECT MyName FROM Mytable” statement.

how do get the full table data?
Using the “SELECT * FROM Mytable” statement.

how do get different or distinct values?
Using the “SELECT DISTINCT ” statement.

how to sort the result?
Using ORDER BY keyword.

how to select a value within a range?
Using the BETWEEN keyword.

how to search for a specific pattern?
Using the LIKE keyword.

how to create a table in the database?
Using the CREATE TABLE keyword.

What does TRIM() Function?
It Removes spaces from a string.

What does the CONCAT() Function?
It Adds two strings together.

What does the LEN() Function?
It returns the length of the string.

What does the REPLICATE() Function?
It repeats the same string.

Related: 20 best examples of SQL like operator