Sunday, January 24, 2016

SUM IN SQL: Solve For The Sum of A Column From Database

Functions are keywords in SQL used to manipulate values in database for output purposes. A function is a command normally used together with a column name that processes the data to produce an output. SQL contains several types of functions. SQL Aggregate functions can perform useful functions such as COUNT, SUM, AVG, MAX, MIN.

Today, we will be discussing about the SUM function. Instead of solving for the sum of a certain column using PHP or other programming language, you can achieve it by using the SQL SUM function in your query.

Let's say you have the following data on your SALES table and you will solve for the total sales.

Query SUM Function

select SUM(total) from sales;

Output  SUM Function

SUM(total) is not quite a straight forward name for the total sales. You can used an alias to organize your output easily by using the keyword as

Query using alias
select SUM(total) as TotalSales from sales

The TotalSales on the above query there is you alias for the sum of all total. Or you can remove the as word and the output will still be the same.


Hope this helps! Feel free to post a comment below for any questions. Thank you! ^_^

SUM IN SQL: Solve For The Sum of A Column  From Database

No comments:

Post a Comment