Code has been added to clipboard!

The SQL SUM() Function and How to Use It

Reading time 1 min
Published Aug 9, 2017
Updated Oct 8, 2019

Introducing the SQL SUM() Function

SQL SUM() is an aggregate function that allows you to get the sum of the selected numeric values.

Syntax for SUM() in SQL

To perform summation in SQL, we need to first select the values we need to add. To do that, we use the SELECT statement:

Example
SELECT SUM(col_name)
FROM tbl_name
WHERE conditions;

In the syntax example above, you see three parameters:

  • col_name defines the name of the column
  • tbl_name defines the name of the table
  • conditions defines specific conditions to filter particular values
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Summation in SQL: an Example

To understand how to use SUM() in SQL better, we'll show you a simple example using a demo database.

A Demo Database

We will be using a part of the table called Orders:

ID Name Count Price Date
1 Apple 120 11.9 2017-08-18
2 Milk 50 9.1 2016-12-19
3 Bread 60 15.4 2017-06-21
4 Water 150 12.3 2017-07-10
5 Chocolate 85 21.9 2016-12-25

Using SQL SUM()

In the code example below, we take the data records from the Price column in the Orders table and find out the total sum:

Example
SELECT SUM(Price)
FROM Orders;

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 28/02/2021
3176 People Used
Only 97 Left
Rating
5.0