Retrieving Column Totals : Sum « Math « SQL / MySQL






Retrieving Column Totals

/*
mysql> SELECT SUM(Credits) AS TotalCredits FROM Course;
+--------------+
| TotalCredits |
+--------------+
|           13 |
+--------------+
1 row in set (0.02 sec)


*/       

/* Create the table */
Drop TABLE Course;

CREATE TABLE Course (
   CourseID INT NOT NULL PRIMARY KEY,
   Name     VARCHAR(50),
   Credits  INT)
TYPE = InnoDB;

/* Insert data */
INSERT INTO Course (CourseID,Name,Credits) VALUES (1,'Java',5);
INSERT INTO Course (CourseID,Name,Credits) VALUES (2,'C#',5);
INSERT INTO Course (CourseID,Name,Credits) VALUES (3,'JavaScript',3);

/* Real command */  
SELECT SUM(Credits) AS TotalCredits FROM Course;
           
       








Related examples in the same category

1.Use SUM and alias
2.Use SUM and alias 2
3.Simplest SUM
4.Another SUM
5.Use AVG and SUM together
6.Do calculation with SUM
7.Use SUM on int value