EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQLite Tutorial SQLite sum
Secondary Sidebar
Python Books

SQL Between Dates

Engineering Applications of Artificial Intelligence

Examples of Data Visualizations

MySQL AB

MDF File in SQL Server

SQLite sum

SQLite sum

Definition on SQLite sum function

SQLite provides the different kinds of aggregate functions to the users, in which that SQLite sum () function is one of the functions that are used to return the sum of all non-null values from the group. If the column contains null values then SQLite returns the null result. SQLite sum () function is work similar to the MySQL sum () function. The non-standard total () function provides an efficient way to work with the different kinds of design in SQL language. The one more important thing about the SQLite total () function is that it always returns the floating result.

Syntax:

Select sum ([all | distinct] specified expression) from specified table name [where specified condition] [group by specified expression];

Explanation:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

In the above syntax, we use sum () function with different parameters as follows.

all: Normally sum () function uses all clauses that are present in SQLite, that means sum () function performs the calculation whether values are duplicate values or not.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

distinct: Sometimes we need to calculate the sum of unique values then we can use a distinct keyword in the SQL statement as per requirement and it is an optional part of syntax.

specified expression: Specified expression means that SQL statement.

specified table name: This actual table name that we need to fetch records from.

where specified condition: We can provide any specified condition to calculate sum of columns as per user requirement and it is an optional part of this syntax.

group by specified expression: Sometimes we need to calculate the sum of columns on their group so that we can use group by clause as per user requirement and it is an optional part of this syntax.

How sum function work in SQLite?

Now let’s see how sum () function works in SQLite as follows.

The output of the SUM () function is an integer number if all info non-NULL values are integer numbers. In the event that any info value is neither a number nor a NULL value, the consequence of the SUM () function is floating point value. The outcome of the SUM () functions is NULL if and just if all info values are NULL. In the event that there is an integer number flood mistake occurs and all info values are NULL or integer, the SUM () function shows an overflow exception message.

The total () and sum () functions return the sum of all non-NULL values from the group. On the off chance that there are no non-NULL info rows, at that time sum () function returns NULL however total () function returns 0.0. Invalid isn’t ordinarily a supportive outcome for the amount of no lines except for the SQL standard requires it and most other SQL information base motors carry out total() that way so SQLite does it similarly to be viable. The non-standard total () function is given as an advantageous method to work around this plan issue in the SQL language. The outcome of total () function is consistently a floating-point value. The outcome of the sum () function is an integer value if all non-NULL sources of info are integer numbers. In the event that any contribution to sum () function is either a number or a NULL total (,) function returns a floating-point value which may be estimation to the genuine total.

Examples

Now let’s see the different examples of sum () function as follows.

First, we need to create a table, so create a table by using the following statement as follows.

create table emp (emp_id integer primary key, emp_name text not null, emp_email text not null, emp_dept text not null, emp_salary float not null);

Explanation

In above example, we created new table name as emp different attribute such as emp_id with integer data type and primary key, emp_name with text data type and not null, emp_email with text data type and not null and emp_salary with float data type and not null constraint and emp_dept with text data type and not null constraint. End output of the above statement as shown in below screenshot as follows.

.table

SQLite sum 1

Now insert some records into the emp table by using insert into statement as follows.

insert into emp (emp_id, emp_name, emp_email, emp_dept, emp_salary) values (1, “Jay”, “jay@gmail.com”, “COMP”, 15000), (2, “Johan”, “jon@gamil.com”, “IT”, 12000), (3, “Jenny”,”jenny@gmail.com”, “COMP”, 20000), (4, “Sameer”, “sam@gmail.com”, “MECH”, 25000), (5, “Pooja”, “pooja@gamil.com”, “IT”, 19000), (6, “Mark”, “mark@gmail.com”, “MECH”, 24000);
select * from emp;

Explanation

With the help of the above statement, we inserted some records into the stud table successfully. End output of above Statement as shown in below screenshot as follows.

SQLite sum 2

Now perform sum () function as follows.

select sum (emp_salary) from emp;

Explanation

In the above example, we use sum () function, in this example, we need to calculate the total salary of emp at that we can use the above statement. End output of above Statement as shown in below screenshot as follows.

SQLite sum 3

Now let’s see how we can use the sum () function with the group by clause as follows.

select emp_dept, sum(emp_salary) from emp group by emp_dept;

Explanation

In the above example, we use sum () function with group by clause as shown in above statement. Suppose we need to calculate sum emp_salary in department wise at that time we can use the above statement. End output of above Statement as shown in below screenshot as follows.

SQLite sum 4

Now let’s see how we can use the where with sum () function as follows.

select emp_salary sum(emp_salary) from emp where emp_dept = “COMP”;

Explanation

In the above example, we use sum () function with where clause as shown in above statement. Suppose we need to find out the total salary of the COMP department at that time we can use the above statement. End output of above Statement as shown in below screenshot as follows.

SQLite sum 5

Similarly, we can use sum () function with join and having as per user requirement.

Conclusion

We hope from this article you have understood about the SQLite sum function. From the above article, we have learned the basic syntax sum function and we also see different examples of sum function. From this article, we learned how and when we use the SQLite sum function.

Recommended Articles

This is a guide to SQLite sum. Here we discuss the Introduction, syntax, how sum () function works in SQLite, and examples. You may also have a look at the following articles to learn more –

  1. SQL Clear Table
  2. SQL Delete View
  3. SQL DESCRIBE TABLE
  4. SQL BLOB
Popular Course in this category
SQLite Tutorial (3 Courses, 1 Project)
  3 Online Courses |  1 Hands-on Projects |  11+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more