Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

MySQL

MySQL Select SUM() with Example

In SQL language, the SUM() aggregation function allows calculating the total sum of a column containing numerical values. This function only works on columns of numeric types (INT, FLOAT …) and does not sum NULL values.
 

Syntax:

Here is the syntax to use this SQL function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT SUM(column_name) FROM table
SELECT SUM(column_name) FROM table
SELECT SUM(column_name) FROM table

This SQL query allows to calculate the sum of the values contained in the column “column_name”.

Note: It is possible to filter the records with WHERE command to calculate the sum of the desired elements only.
 
[st_adsense]  

Example :

Let’s suppose a system that manages invoices and records each purchase in a database. This system uses an “invoice” table containing one row for each product. The table looks like the example below:

+------+----------+------------+-------+---------+
|  id  |   name   | invoice_id | stock |  price  |
+------+----------+------------+-------+---------+
|  101 | Monitor  | 1          |  45   | 100     |
|  102 | CD-ROM   | 1          |  100  | 40      |
|  103 | Modem    | 1          |  12   | 20      |
|  104 | Speaker  | 2          |  11   | 3       |
|  105 | Pencil   | 3          |  148  | 2       |
|  106 | Gum      | 4          |  250  | 1       |
+------+----------+------------+-------+---------+

 

Sum function with WHERE:

To calculate the sum of the invoice n°1 it is possible to use the following SQL query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT SUM(price) AS total_price
FROM invoice
WHERE invoice_id = 1
SELECT SUM(price) AS total_price FROM invoice WHERE invoice_id = 1
SELECT SUM(price) AS total_price
FROM invoice
WHERE invoice_id = 1

Output:

160

This result shows that all the items purchased on invoice n°1 represent an amount of 160$ (sum of 100$ + 40$ + 20$).
 
[st_adsense]  

Sum with GROUP BY

To calculate the sum of each invoice, it is possible to group the rows based on the “invoice_id” column. Such a result can be achieved by using the following query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT invoice_id, SUM(price) AS total_price
FROM invoice
GROUP BY invoice_id
SELECT invoice_id, SUM(price) AS total_price FROM invoice GROUP BY invoice_id
SELECT invoice_id, SUM(price) AS total_price
FROM invoice
GROUP BY invoice_id

Output:

+---------------+-------------+
|  invoice_id   | total_price |
+---------------+-------------+
|   1           | 160         |
|   2           | 3           |
|   3           | 2           |
|   4           | 1           |
+---------------+-------------+

This result shows that it is possible to determine the total price of each invoice using the SUM() function.

mcq

Leave a Reply

Your email address will not be published. Required fields are marked *