{"id":976,"date":"2024-12-15T16:08:14","date_gmt":"2024-12-15T09:08:14","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=976"},"modified":"2025-01-22T21:33:29","modified_gmt":"2025-01-22T14:33:29","slug":"postgresql-sum","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/","title":{"rendered":"PostgreSQL SUM Aggregate Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>SUM<\/code> aggregate function to return the sum of a set of values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-sum-aggregate-function'>Getting Started with the PostgreSQL SUM Aggregate Function <a href=\"#getting-started-with-the-postgresql-sum-aggregate-function\" class=\"anchor\" id=\"getting-started-with-the-postgresql-sum-aggregate-function\" title=\"Anchor for Getting Started with the PostgreSQL SUM Aggregate Function\">#<\/a><\/h2>\n\n\n\n<p>The <code>SUM()<\/code> function takes a set of values and returns their total.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>SUM<\/code> function that calculates the sum of values in a table column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  SUM(column1)\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>SUM<\/code> will calculate the total values in <code>column1<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note:<\/strong> The <code>SUM<\/code> function ignores <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-not-null\/\">NULL<\/a><\/code> in the calculation. If <code>column1<\/code> has no value, the <code>SUM()<\/code> function returns <code>NULL<\/code>, not zero.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-sum-with-group-by'>Using SUM with GROUP BY <a href=\"#using-sum-with-group-by\" class=\"anchor\" id=\"using-sum-with-group-by\" title=\"Anchor for Using SUM with GROUP BY\">#<\/a><\/h3>\n\n\n\n<p>In practice, you often want to calculate the totals for groups of rows. To do that, you use the <code>SUM<\/code> function with the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\">GROUP BY<\/a><\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  SUM(column1),\n  column2\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  column2;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>GROUP BY<\/code> clause groups rows by the values in <code>column2<\/code> into groups.<\/li>\n\n\n\n<li>The <code>SUM<\/code> function calculates the total value in <code>column1<\/code> for each group.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-sum-aggregate-function-examples'>PostgreSQL SUM Aggregate Function Examples <a href=\"#postgresql-sum-aggregate-function-examples\" class=\"anchor\" id=\"postgresql-sum-aggregate-function-examples\" title=\"Anchor for PostgreSQL SUM Aggregate Function Examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s explore some examples of using the <code>SUM<\/code> function with the <code>inventories<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='finding-the-total-quantity-in-the-inventory'>Finding the Total Quantity in the Inventory <a href=\"#finding-the-total-quantity-in-the-inventory\" class=\"anchor\" id=\"finding-the-total-quantity-in-the-inventory\" title=\"Anchor for Finding the Total Quantity in the Inventory\">#<\/a><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"139\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png\" alt=\"\" class=\"wp-image-1046\"\/><\/figure>\n\n\n\n<p>The following <code>SELECT<\/code> statement uses the <code>SUM()<\/code> aggregate function to find the total inventory quantity from the <code>inventories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIFNVTShxdWFudGl0eSkgRlJPTSBpbnZlbnRvcmllczs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> sum\n<span class=\"hljs-comment\">------<\/span>\n <span class=\"hljs-number\">5360<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>SUM<\/code> function adds up all values in the <code>quantity<\/code> column of the <code>inventories<\/code> table and returns a single value.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id='finding-the-total-quantity-in-the-inventory-for-each-group'>Finding the Total Quantity in the Inventory for Each Group <a href=\"#finding-the-total-quantity-in-the-inventory-for-each-group\" class=\"anchor\" id=\"finding-the-total-quantity-in-the-inventory-for-each-group\" title=\"Anchor for Finding the Total Quantity in the Inventory for Each Group\">#<\/a><\/h3>\n\n\n\n<p>To find the total quantity of each warehouse, you can use the <code>SUM<\/code> function with the <code><a href=\"http:\/\/GROUP BY\">GROUP BY<\/a><\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  warehouse_id,\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHdhcmVob3VzZV9pZCwgU1VNKHF1YW50aXR5KSBGUk9NIGludmVudG9yaWVzIEdST1VQIEJZIHdhcmVob3VzZV9pZDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> warehouse_id | sum\n<span class=\"hljs-comment\">--------------+------<\/span>\n            <span class=\"hljs-number\">3<\/span> | <span class=\"hljs-number\">1780<\/span>\n            <span class=\"hljs-number\">2<\/span> | <span class=\"hljs-number\">1690<\/span>\n            <span class=\"hljs-number\">1<\/span> | <span class=\"hljs-number\">1890<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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 groups rows in the <code>inventories<\/code> by the values in the <code>warehouse_id<\/code> column.<\/li>\n\n\n\n<li>The <code>SUM<\/code> function calculates the total quantity for each group.<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<p>To retrieve the warehouse name instead of the id, you can join the <code>inventories<\/code> table with the <code>warehouses<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"344\" height=\"139\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories-warehouses.png\" alt=\"\" class=\"wp-image-1045\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories-warehouses.png 344w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories-warehouses-300x121.png 300w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/figure>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  warehouse_name,\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n  <span class=\"hljs-keyword\">JOIN<\/span> warehouses <span class=\"hljs-keyword\">USING<\/span> (warehouse_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHdhcmVob3VzZV9uYW1lLCBTVU0ocXVhbnRpdHkpIEZST00gaW52ZW50b3JpZXMgSk9JTiB3YXJlaG91c2VzIFVTSU5HICh3YXJlaG91c2VfaWQpIEdST1VQIEJZIHdhcmVob3VzZV9uYW1lOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">     warehouse_name      | sum\n<span class=\"hljs-comment\">-------------------------+------<\/span>\n San Francisco Warehouse | <span class=\"hljs-number\">1690<\/span>\n Los Angeles Warehouse   | <span class=\"hljs-number\">1780<\/span>\n San Jose Warehouse      | <span class=\"hljs-number\">1890<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, you retrieve the total quantity of items per warehouse by grouping the results by <code>warehouse_name<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-sum-in-the-having-clause-to-filter-groups'>Using SUM in the HAVING Clause to Filter Groups <a href=\"#using-sum-in-the-having-clause-to-filter-groups\" class=\"anchor\" id=\"using-sum-in-the-having-clause-to-filter-groups\" title=\"Anchor for Using SUM in the HAVING Clause to Filter Groups\">#<\/a><\/h3>\n\n\n\n<p>To find warehouses with a total quantity greater than <code>1700<\/code>, you can use the <code>SUM<\/code> function in the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-having\/\">HAVING<\/a><\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  warehouse_name,\n  SUM(quantity)\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventories\n  <span class=\"hljs-keyword\">JOIN<\/span> warehouses <span class=\"hljs-keyword\">USING<\/span> (warehouse_id)\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  warehouse_name\n<span class=\"hljs-keyword\">HAVING<\/span> \n  SUM(quantity) &gt; <span class=\"hljs-number\">1700<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHdhcmVob3VzZV9uYW1lLCBTVU0ocXVhbnRpdHkpIEZST00gaW52ZW50b3JpZXMgSk9JTiB3YXJlaG91c2VzIFVTSU5HICh3YXJlaG91c2VfaWQpIEdST1VQIEJZIHdhcmVob3VzZV9uYW1lIEhBVklORyBTVU0ocXVhbnRpdHkpID4gMTcwMDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">    warehouse_name     | sum\n<span class=\"hljs-comment\">-----------------------+------<\/span>\n Los Angeles Warehouse | <span class=\"hljs-number\">1780<\/span>\n San Jose Warehouse    | <span class=\"hljs-number\">1890<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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 the rows in the <code>inventories<\/code> table by warehouse names.<\/li>\n\n\n\n<li>The <code>SUM<\/code> calculates the total quantity for each group.<\/li>\n\n\n\n<li>The <code>HAVING<\/code> clause keeps the warehouses with a quantity greater than <code>1700<\/code>.<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>SUM()<\/code> function to return the total value of a set.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=sum\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\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=\"976\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL SUM Aggregate 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=\"976\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL SUM Aggregate 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>Summary: In this tutorial, you&#8217;ll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values. Getting Started with the PostgreSQL SUM Aggregate Function # The SUM() function takes a set of values and returns their total. Here&#8217;s the syntax of the SUM function that calculates the sum [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":950,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-976","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>PostgreSQL SUM Aggregate Function<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values.\" \/>\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.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL SUM Aggregate Function\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-22T14:33:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png\" \/>\n\t<meta property=\"og:image:width\" content=\"144\" \/>\n\t<meta property=\"og:image:height\" content=\"139\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/\",\"name\":\"PostgreSQL SUM Aggregate Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/inventories.png\",\"datePublished\":\"2024-12-15T09:08:14+00:00\",\"dateModified\":\"2025-01-22T14:33:29+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/inventories.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/inventories.png\",\"width\":144,\"height\":139},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/postgresql-sum\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Aggregate Functions\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-aggregate-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL SUM Aggregate Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?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":"PostgreSQL SUM Aggregate Function","description":"In this tutorial, you'll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values.","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.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL SUM Aggregate Function","og_description":"In this tutorial, you'll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-22T14:33:29+00:00","og_image":[{"width":144,"height":139,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/","url":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/","name":"PostgreSQL SUM Aggregate Function","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","datePublished":"2024-12-15T09:08:14+00:00","dateModified":"2025-01-22T14:33:29+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL SUM aggregate function to return the sum of a set of values.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/inventories.png","width":144,"height":139},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/postgresql-sum\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Aggregate Functions","item":"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL SUM Aggregate Function"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=976"}],"version-history":[{"count":9,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/976\/revisions"}],"predecessor-version":[{"id":1798,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/976\/revisions\/1798"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/950"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}