{"id":2434,"date":"2013-05-16T23:15:04","date_gmt":"2013-05-17T07:15:04","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2434"},"modified":"2023-11-09T17:34:59","modified_gmt":"2023-11-10T00:34:59","slug":"mysql-sum","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/","title":{"rendered":"MySQL SUM() function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>SUM()<\/code> function to calculate the sum of values in a set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the MySQL SUM() function<\/h2>\n\n\n\n<p>The <code>SUM()<\/code> function&nbsp;is an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/\">aggregate function<\/a> that allows you to calculate the sum of values in a set. The syntax of the <code>SUM()<\/code> function is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">SUM(DISTINCT expression)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is how the <code>SUM()<\/code> function works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you use the <code>SUM()<\/code> function in a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT<\/a><\/code> statement that returns no row, the <code>SUM()<\/code> function returns <code>NULL<\/code>, not zero.<\/li>\n\n\n\n<li>The <code>DISTINCT<\/code> option directs the <code>SUM()<\/code> function to calculate the sum of unique values in a set.<\/li>\n\n\n\n<li>The <code>SUM()<\/code> function ignores the <code>NULL<\/code> values in the calculation.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL SUM() function illustration<\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/perl-mysql\/perl-mysql-create-table\/\">create a new table<\/a> called <code>sum_demo<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> sum_demo (\n    n <span class=\"hljs-built_in\">INT<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>sum_demo<\/code> table includes one column called <code>n<\/code> with the type <code>INT<\/code>.<\/p>\n\n\n\n<p>Then, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">insert some rows<\/a> into the <code>sum_demo<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> sum_demo(n) \n<span class=\"hljs-keyword\">VALUES<\/span> \n  (<span class=\"hljs-number\">1<\/span>), \n  (<span class=\"hljs-number\">1<\/span>), \n  (<span class=\"hljs-number\">2<\/span>), \n  (<span class=\"hljs-literal\">NULL<\/span>), \n  (<span class=\"hljs-number\">3<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, calculate the total values in the <code>n<\/code> column using the <code>SUM()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(n)\n<span class=\"hljs-keyword\">FROM<\/span>\n    sum_demo;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"69\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-all-rows-demo.png\" alt=\"\" class=\"wp-image-7769\"\/><\/figure>\n\n\n\n<p>As you can see, the <code>SUM()<\/code> function calculates the total of 1, 1, 2, and 3. And it ignores NULL.<\/p>\n\n\n\n<p>Finally, calculate the total unique values in the <code>n<\/code> column using the <code>SUM()<\/code> function with the <code>DISTINCT<\/code> option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> n)\n<span class=\"hljs-keyword\">FROM<\/span>\n    sum_demo;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"119\" height=\"47\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-distinct-example.png\" alt=\"\" class=\"wp-image-7770\"\/><\/figure>\n\n\n\n<p>In this case, the <code>SUM()<\/code> with the <code>DISTINCT<\/code> option calculates the sum of unique values which are 1, 2, and 3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL SUM() function examples<\/h2>\n\n\n\n<p>We&#8217;ll use the table <code>orderdetails<\/code> from the <a title=\"MySQL Sample Database\" href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/orderdetails.svg\" alt=\"\" class=\"wp-image-10768\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1) Simple MySQL SUM() function example<\/h3>\n\n\n\n<p>This example uses the <code>SUM()<\/code> function to get the total number of items of the order details:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered) SalesQuantity\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"103\" height=\"39\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-example.png\" alt=\"\" class=\"wp-image-7768\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) MySQL SUM() function with expression example<\/h3>\n\n\n\n<p>The following shows the order line items of the order number <code>10110<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    orderNumber, \n    quantityOrdered, \n    priceEach\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n<span class=\"hljs-keyword\">WHERE<\/span>\n    orderNumber = <span class=\"hljs-number\">10100<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"255\" height=\"97\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-order-details.png\" alt=\"\" class=\"wp-image-7760\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-order-details.png 255w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-order-details-200x76.png 200w\" sizes=\"auto, (max-width: 255px) 100vw, 255px\" \/><\/figure>\n\n\n\n<p>To calculate the total for the order number <code>10110<\/code>, you use the <code>SUM()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\t<span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach)  orderTotal\n<span class=\"hljs-keyword\">FROM<\/span>\n\torderdetails\n<span class=\"hljs-keyword\">WHERE<\/span>\n\torderNumber = <span class=\"hljs-number\">10100<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"87\" height=\"36\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-order-total.png\" alt=\"\" class=\"wp-image-7761\"\/><\/figure>\n\n\n\n<p>In this example, the <code>SUM()<\/code> function calculates the total of the following expression of all order line items of order number 10110:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">quantityOrdered * priceEach<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3) MySQL SUM() with the GROUP BY clause example<\/h3>\n\n\n\n<p>The <code>SUM()<\/code> function is often used with the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\">GROUP BY<\/a><\/code> clause to calculate the sum for each group.<\/p>\n\n\n\n<p>For example, you can calculate the total amount of each order by using the <code>SUM()<\/code> function with the <code>GROUP BY<\/code> clause as shown in the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    orderNumber, \n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) orderTotal\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    orderNumber\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    orderTotal <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7762\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-group-by.png\" alt=\"\" width=\"164\" height=\"240\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-group-by.png 164w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-group-by-137x200.png 137w\" sizes=\"auto, (max-width: 164px) 100vw, 164px\" \/><\/h3>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>GROUP BY<\/code> clause divides order details into groups grouped by the order number.<\/li>\n\n\n\n<li>The <code>SUM()<\/code> function calculates the total of each amount in each order.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4) MySQL SUM() with HAVING clause example<\/h3>\n\n\n\n<p>You can use the <code>SUM()<\/code> function in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-having\/\">HAVING<\/a><\/code> clause to filter the group. This example illustrates how to select orders whose order amounts are greater than <code>60,000<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    orderNumber, \n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) orderTotal\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    orderNumber\n<span class=\"hljs-keyword\">HAVING<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) &gt; <span class=\"hljs-number\">60000<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    orderTotal;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"168\" height=\"77\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-having.png\" alt=\"\" class=\"wp-image-7763\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5) MySQL SUM() with NULL example<\/h3>\n\n\n\n<p>The <code>SUM()<\/code> function returns <code>NULL<\/code> if the result set is empty. Sometimes, you may want the <code>SUM()<\/code> function to return zero instead of <code>NULL<\/code>.<\/p>\n\n\n\n<p>In this case, you can use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-comparison-functions\/mysql-coalesce\/\">COALESCE()<\/a><\/code> function. The <code>COALESCE<\/code> function accepts two arguments and returns the second argument if the first argument is <code>NULL<\/code>; otherwise, it returns the first argument.<\/p>\n\n\n\n<p>See the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">COALESCE<\/span>(<span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach), <span class=\"hljs-number\">0<\/span>) <span class=\"hljs-keyword\">result<\/span>\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n<span class=\"hljs-keyword\">WHERE<\/span>\n    productCode = <span class=\"hljs-string\">'S1_20'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"65\" height=\"36\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-coalesce.png\" alt=\"\" class=\"wp-image-7766\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">6) MySQL SUM() with join example<\/h3>\n\n\n\n<p>See the following <code>orders<\/code> and <code>orderdetails<\/code> tables:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"194\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/orders_order_details_tables.png\" alt=\"MySQL Transaction: orders &amp; orderDetails Tables\" class=\"wp-image-3827\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/orders_order_details_tables.png 444w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2009\/12\/orders_order_details_tables-300x131.png 300w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/figure>\n\n\n\n<p>You can use the <code>SUM()<\/code> function in a <code>SELECT<\/code> with <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-join\/\">JOIN<\/a><\/code> clause to calculate the sum of values in a table based on a condition specified by the values in another table.<\/p>\n\n\n\n<p>This statement uses the <code>SUM()<\/code> function to calculate the total amounts of the canceled orders:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) cancelled_amount\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> orders <span class=\"hljs-keyword\">USING<\/span> (orderNumber)\n<span class=\"hljs-keyword\">WHERE<\/span> \n    <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Cancelled'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">7) MySQL SUM IF example<\/h3>\n\n\n\n<p>The following statement uses the <code>SUM()<\/code> function to calculate the number of items sold for each order status:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">status<\/span>, \n    <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered)\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    orders <span class=\"hljs-keyword\">USING<\/span> (orderNumber)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">status<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"215\" height=\"134\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-if.png\" alt=\"\" class=\"wp-image-7773\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-if.png 215w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-if-200x125.png 200w\" sizes=\"auto, (max-width: 215px) 100vw, 215px\" \/><\/figure>\n\n\n\n<p>If you want to rotate rows to columns, you can use the <code>SUM()<\/code> function with <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-control-flow-functions\/mysql-case-function\/\">CASE<\/a><\/code> expression. It is kind of <code>SUMIF<\/code> logic:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Shipped'<\/span> <span class=\"hljs-keyword\">THEN<\/span> quantityOrdered\n    <span class=\"hljs-keyword\">END<\/span>) qty_shipped,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Resolved'<\/span> <span class=\"hljs-keyword\">THEN<\/span> quantityOrdered\n    <span class=\"hljs-keyword\">END<\/span>) qty_resolved,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Cancelled'<\/span> <span class=\"hljs-keyword\">THEN<\/span> quantityOrdered\n    <span class=\"hljs-keyword\">END<\/span>) qty_cancelled,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'On Hold'<\/span> <span class=\"hljs-keyword\">THEN<\/span> quantityOrdered\n    <span class=\"hljs-keyword\">END<\/span>) qty_on_hold,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Disputed'<\/span> <span class=\"hljs-keyword\">THEN<\/span> quantityOrdered\n    <span class=\"hljs-keyword\">END<\/span>) qty_on_disputed,\n    <span class=\"hljs-keyword\">SUM<\/span>(<span class=\"hljs-keyword\">CASE<\/span>\n        <span class=\"hljs-keyword\">WHEN<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'In Process'<\/span> <span class=\"hljs-keyword\">THEN<\/span> quantityOrdered\n    <span class=\"hljs-keyword\">END<\/span>) qty_in_process\n<span class=\"hljs-keyword\">FROM<\/span>\n    orderdetails\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    orders <span class=\"hljs-keyword\">USING<\/span> (orderNumber);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sumif-example.png\" alt=\"\" class=\"wp-image-7774\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sumif-example.png 524w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sumif-example-200x15.png 200w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the MySQL <code>SUM()<\/code> function to calculate the sum of values in a set.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2434\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\"\n\t\t\t\tdata-post-title=\"MySQL SUM() function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2434\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\"\n\t\t\t\tdata-post-title=\"MySQL SUM() function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":509,"menu_order":6,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2434","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL SUM Function<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL SUM Function\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T00:34:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-all-rows-demo.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/\",\"name\":\"MySQL SUM Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/mysql-sum-all-rows-demo.png\",\"datePublished\":\"2013-05-17T07:15:04+00:00\",\"dateModified\":\"2023-11-10T00:34:59+00:00\",\"description\":\"This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/mysql-sum-all-rows-demo.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/mysql-sum-all-rows-demo.png\",\"width\":69,\"height\":38},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-sum\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Aggregate Functions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL SUM() function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL SUM Function","description":"This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/","og_locale":"en_US","og_type":"article","og_title":"MySQL SUM Function","og_description":"This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-11-10T00:34:59+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-all-rows-demo.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/","url":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/","name":"MySQL SUM Function","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-all-rows-demo.png","datePublished":"2013-05-17T07:15:04+00:00","dateModified":"2023-11-10T00:34:59+00:00","description":"This tutorial shows you how to use the MySQL SUM function to calculate the sum of a set of values or an expression.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-all-rows-demo.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-sum-all-rows-demo.png","width":69,"height":38},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Aggregate Functions","item":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/"},{"@type":"ListItem","position":3,"name":"MySQL SUM() function"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=2434"}],"version-history":[{"count":3,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2434\/revisions"}],"predecessor-version":[{"id":12234,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2434\/revisions\/12234"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/509"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=2434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}