Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
UPPER() function in SQL Server
Next article icon

SUM() Function in SQL Server

Last Updated : 17 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The SUM() function in SQL Server is an essential aggregate function used to calculate the total sum of values in a numeric column. It aggregates data by summing up all values in the specified column for the rows that match the criteria of the query.

In this article, We will learn about SUM() Function in SQL Server by understanding various examples and so on.

SUM() Function in SQL Server

  • The SUM() function in SQL Server is an aggregate function used to calculate the total sum of a numeric columns values.
  • It adds up all the values in a given column for the rows selected by the query.

Features of SUM() Function in SQL Server

  • This function is used to compute the sum of the specified group of values.
  • This function comes under Numeric Functions.
  • This function accepts only one parameter namely expression.
  • This function ignores the null value.

Syntax:

SUM(expression)

Parameter:

This method accepts one parameter.

  • expression: A specified expression which can either be a field or a given formula.

Returns:

It returns the sum of the specified group of values.

Examples of SUM() Function in SQL Server

Example 1:

Using SUM() function and getting the output.

CREATE TABLE product
(
user_id int IDENTITY(100, 2) NOT NULL,
product_1 VARCHAR(10),
product_2 VARCHAR(10),
price int
);
INSERT product(product_1, price)
VALUES ('rice', 400);

INSERT product(product_2, price)
VALUES ('grains', 600);

SELECT SUM(price) FROM product;

Output:

1000

Example 2:

Using SUM() function and finding the sum of all the stated float values.

CREATE TABLE floats
(
user_id int IDENTITY(100, 2) NOT NULL,
float_val float
);
INSERT floats(float_val)
VALUES (3.6);

INSERT floats(float_val)
VALUES (2.1);

INSERT floats(float_val)
VALUES (6.3);

INSERT floats(float_val)
VALUES (9.0);

INSERT floats(float_val)
VALUES (7.0);

SELECT SUM(float_val) FROM floats;

Output:

28

Example 3:

Using SUM() function and getting the output where MRP is less than the sum of all the MRP’s.

CREATE TABLE package
(
user_id int IDENTITY(100, 4) NOT NULL,
item VARCHAR(10),
mrp int
);
INSERT package(item, mrp)
VALUES ('book1', 3);

INSERT package(item, mrp)
VALUES ('book2', 350);

INSERT package(item, mrp)
VALUES ('book3', 400);

SELECT * FROM package
WHERE mrp < (SELECT SUM(mrp) FROM package);

Output:

user_id item mrp
100 book1 3
104 book2 350
108 book3 400

Example 4:

Using SUM() function and getting the sum of all the (MRP-sales price) values.

CREATE TABLE package
(
user_id int IDENTITY(100, 4) NOT NULL,
item VARCHAR(10),
mrp int,
sp int
);
INSERT package(item, mrp, sp)
VALUES ('book1', 250, 240);

INSERT package(item, mrp, sp)
VALUES ('book2', 350, 320);

INSERT package(item, mrp, sp)
VALUES ('book3', 400, 350);

SELECT SUM(mrp-sp) FROM package;

Output:

90

Conclusion

The SUM() function is an essential part of SQL Server, enabling developers to quickly and easily compute the total of numeric columns. Whether it’s used for financial calculations, data analysis, or other numeric operations, SUM() plays a vital role in data aggregation.



Next Article
UPPER() function in SQL Server
author
nidhi1352singh
Improve
Article Tags :
  • Databases
  • SQL
  • DBMS-SQL
  • SQL-Server

Similar Reads

  • STR() Function in SQL Server
    The STR() function converts a numeric value to a character value. Syntax : STR(float_expression [, length [, decimal]]) Parameter : This method accepts three parameters as mentioned above and described below : float_expression : It is a numeric expression that evaluates to an approximate number with
    1 min read
  • SQRT() Function in SQL Server
    SQRT() function : This function in SQL Server is used to return the square root of a specified positive number. For example, if the specified number is 81, this function will return 9. Features : This function is used to find the square root of a given number. This function accepts only positive num
    2 min read
  • UPPER() function in SQL Server
    The UPPER() function in SQL Server is a useful tool for converting all characters in a string to uppercase. This function is essential for ensuring uniform text formatting and for performing case-insensitive comparisons. In this article, We will learn about the UPPER() function in SQL Server by unde
    3 min read
  • YEAR() Function in SQL Server
    The YEAR() function in SQL Server is a powerful tool designed to extract the year component from a given date or datetime expression. It allows users to isolate the year as an integer value and facilitating various date-related operations and analyses. In this article, We will learn about the YEAR()
    2 min read
  • UNICODE() Function in SQL Server
    In this article, we are going to cover UNICODE() function where you will see the Unicode for given specific character or for expression character. UNICODE() : It is the function that gets the integer value for the first character of the input expression, as defined by the Unicode standard. Syntax :
    1 min read
  • SQL Server LEN() Function
    SQL SERVER LEN() function calculates the number of characters of an input string, excluding the trailing spaces. LEN() Function in SQL ServerThe LEN function in the SQL Server fetches the number of characters in a string. It counts the preceding spaces but not the trailing spaces. For eg, 'SQL SERVE
    2 min read
  • LOG() Function in SQL Server
    The LOG() function returns the logarithm of a specified number or the logarithm of the number to the specified base. Syntax : LOG(number, base) Parameter : LOG() function accepts two-parameters as mentioned above and described below. number - This parameter hold a number which is greater than 0. bas
    1 min read
  • AVG() Function in SQL Server
    The AVG() function in SQL Server is an essential aggregate function used to compute the average value of a numeric column. It works by summing all non-NULL values in the specified column and then dividing the total by the number of these values. In this article, We will learn about AVG() Function in
    4 min read
  • MIN() Function in SQL Server
    MIN() : This function in SQL Server is used to find the value that is minimum in the group of values stated. Features : This function is used to find the minimum value.This function comes under Numeric Functions.This function accepts only one parameter namely expression. Syntax : MIN(expression) Par
    2 min read
  • DAY() Function in SQL Server
    DAY() function : This function in SQL Server is used to return the day of the month i.e, from 1st to 31st for date stated. Features : This function is used to find the day of the month for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, date.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences