The SUM Function in SQL

Leave a Comment

You use the SUM function if you have a particular numeric field in a database table that you want to sum for all records. Imagine that you have a Scores table to track how your class of third-grade students is progressing.

Each individual record you enter into this table has a value entered into the Point field (in this case, the Point field is used to record the student’s score on each quiz, test, and so on). In the following example, the executed query returns the sum of all points for all students:
SELECT SUM (POINT) FROM SCORES

Although this type of all-inclusive query is useful when you want to see the grand total points achieved by all your students, you use the SUM function to return the total number of points for each student in the table. That said, consider the following query:
SELECT SUM (POINT) FROM SCORES WHERE STUDENT_ID=15

In this example, only the records where the Student_ID field equals 15 are total and again, only the Point field for each record is totaled.

The SUM function can be used only on fields that have a numeric value.

0 comments:

Post a Comment