{"id":2114,"date":"2025-02-16T09:21:16","date_gmt":"2025-02-16T02:21:16","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=2114"},"modified":"2025-02-16T09:22:37","modified_gmt":"2025-02-16T02:22:37","slug":"postgresql-cube","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/","title":{"rendered":"PostgreSQL CUBE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PostgreSQL <code>CUBE<\/code> to generate all possible aggregations of the specified columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-cube'>Introduction to PostgreSQL CUBE <a href=\"#introduction-to-postgresql-cube\" class=\"anchor\" id=\"introduction-to-postgresql-cube\" title=\"Anchor for Introduction to PostgreSQL CUBE\">#<\/a><\/h2>\n\n\n\n<p>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\">GROUP BY<\/a><\/code> clause allows you to group rows and calculate an <a href=\"https:\/\/www.pgtutorial.com\/postgresql-aggregate-functions\/\">aggregation<\/a> of a single grouping set.<\/p>\n\n\n\n<p>To calculate aggregations of all possible combinations of a set of columns, you can use the <code>GROUP BY<\/code> clause with the <code>CUBE<\/code> option.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>GROUP BY<\/code> with the <code>CUBE<\/code>:<\/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  column1,\n  column2,\n  aggregate_function (column3)\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  <span class=\"hljs-keyword\">CUBE<\/span> (column1, column2);<\/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, <code>CUBE<\/code> will generate the following groupings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>(column1, column2)<\/code>: Individual groups by <code>column1<\/code> and <code>column2<\/code>.<\/li>\n\n\n\n<li><code>(column1, NULL)<\/code>: Groups for values in <code>column2<\/code>.<\/li>\n\n\n\n<li><code>(NULL, column2)<\/code>: Groups for values in <code>column1<\/code>.<\/li>\n\n\n\n<li><code>(NULL, NULL)<\/code>: A group for all columns.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-cube-examples'>PostgreSQL CUBE Examples <a href=\"#postgresql-cube-examples\" class=\"anchor\" id=\"postgresql-cube-examples\" title=\"Anchor for PostgreSQL CUBE Examples\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>inventory_reports<\/code> table to demonstrate the <code>GROUP BY CUBE<\/code>:<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Here\u2019s the script for generating inventory_reports table:<\/summary><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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> inventory_reports(\n     warehouse <span class=\"hljs-type\">VARCHAR<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n     brand <span class=\"hljs-type\">VARCHAR<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n     quantity <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> inventory_reports(warehouse, brand, quantity)\n<span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'San Jose'<\/span>, <span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">100<\/span>),\n(<span class=\"hljs-string\">'San Francisco'<\/span>, <span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">200<\/span>),\n(<span class=\"hljs-string\">'Texas'<\/span>, <span class=\"hljs-string\">'Apple'<\/span>, <span class=\"hljs-number\">300<\/span>),\n(<span class=\"hljs-string\">'San Jose'<\/span>, <span class=\"hljs-string\">'Samsung'<\/span>, <span class=\"hljs-number\">50<\/span>),\n(<span class=\"hljs-string\">'San Francisco'<\/span>, <span class=\"hljs-string\">'Samsung'<\/span>, <span class=\"hljs-number\">100<\/span>),\n(<span class=\"hljs-string\">'Texas'<\/span>, <span class=\"hljs-string\">'Samsung'<\/span>, <span class=\"hljs-number\">150<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/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><\/details>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-postgresql-cube-with-one-column'>Using PostgreSQL CUBE with One Column <a href=\"#using-postgresql-cube-with-one-column\" class=\"anchor\" id=\"using-postgresql-cube-with-one-column\" title=\"Anchor for Using PostgreSQL CUBE with One Column\">#<\/a><\/h3>\n\n\n\n<p>The following query uses a <code>GROUP BY<\/code> clause to calculate the total quantity of each brand:<\/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  brand,\n  SUM(quantity) total_quantity\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventory_reports\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand;<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=inventory-reports&amp;q=U0VMRUNUIGJyYW5kLCBTVU0ocXVhbnRpdHkpIHRvdGFsX3F1YW50aXR5IEZST00gaW52ZW50b3J5X3JlcG9ydHMgR1JPVVAgQlkgYnJhbmQ7\" 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\">  brand  | total_quantity\n<span class=\"hljs-comment\">---------+----------------<\/span>\n Samsung |            <span class=\"hljs-number\">300<\/span>\n Apple   |            <span class=\"hljs-number\">600<\/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>To include a row that calculates the total quantity for all brands, you can use the <code>GROUP BY CUBE<\/code> as follows:<\/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  brand,\n  SUM(quantity) total_quantity\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventory_reports\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  <span class=\"hljs-keyword\">CUBE<\/span> (brand)\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand <span class=\"hljs-keyword\">NULLS LAST<\/span>;<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=inventory-reports&amp;q=U0VMRUNUIGJyYW5kLCBTVU0ocXVhbnRpdHkpIHRvdGFsX3F1YW50aXR5IEZST00gaW52ZW50b3J5X3JlcG9ydHMgR1JPVVAgQlkgQ1VCRSAoYnJhbmQpIE9SREVSIEJZIGJyYW5kIE5VTExTIExBU1Q7\" 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\">  brand  | total_quantity\n<span class=\"hljs-comment\">---------+----------------<\/span>\n Apple   |            <span class=\"hljs-number\">600<\/span>\n Samsung |            <span class=\"hljs-number\">300<\/span> \n <span class=\"hljs-keyword\">NULL<\/span>    |            <span class=\"hljs-number\">900<\/span> -&gt; grand total<\/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, the <code>CUBE<\/code> generates two groupings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>(brand)<\/code>: Calculate the sum of quantity by brand.<\/li>\n\n\n\n<li><code>()<\/code>: Calculate the sum of the quantity of all brands.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='using-cube-with-multiple-columns'>Using CUBE with Multiple Columns <a href=\"#using-cube-with-multiple-columns\" class=\"anchor\" id=\"using-cube-with-multiple-columns\" title=\"Anchor for Using CUBE with Multiple Columns\">#<\/a><\/h3>\n\n\n\n<p>The following query uses the <code>GROUP BY CUBE<\/code> to generate the subtotals and total:<\/p>\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  brand,\n  warehouse,\n  SUM(quantity) total_quantity\n<span class=\"hljs-keyword\">FROM<\/span>\n  inventory_reports\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  <span class=\"hljs-keyword\">CUBE<\/span> (brand, warehouse)\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  brand <span class=\"hljs-keyword\">NULLS LAST<\/span>,\n  warehouse <span class=\"hljs-keyword\">NULLS LAST<\/span>;<\/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<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=inventory-reports&amp;q=U0VMRUNUIGJyYW5kLCB3YXJlaG91c2UsIFNVTShxdWFudGl0eSkgdG90YWxfcXVhbnRpdHkgRlJPTSBpbnZlbnRvcnlfcmVwb3J0cyBHUk9VUCBCWSBDVUJFIChicmFuZCwgd2FyZWhvdXNlKSBPUkRFUiBCWSBicmFuZCBOVUxMUyBMQVNULCB3YXJlaG91c2UgTlVMTFMgTEFTVDs%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\">  brand  |   warehouse   | total_quantity\n<span class=\"hljs-comment\">---------+---------------+----------------<\/span>\n Apple   | San Francisco |            <span class=\"hljs-number\">200<\/span>\n Apple   | San Jose      |            <span class=\"hljs-number\">100<\/span>\n Apple   | Texas         |            <span class=\"hljs-number\">300<\/span>\n Apple   | <span class=\"hljs-keyword\">NULL<\/span>          |            <span class=\"hljs-number\">600<\/span>  \n Samsung | San Francisco |            <span class=\"hljs-number\">100<\/span>\n Samsung | San Jose      |             <span class=\"hljs-number\">50<\/span>\n Samsung | Texas         |            <span class=\"hljs-number\">150<\/span>\n Samsung | <span class=\"hljs-keyword\">NULL<\/span>          |            <span class=\"hljs-number\">300<\/span>  \n <span class=\"hljs-keyword\">NULL<\/span>    | San Francisco |            <span class=\"hljs-number\">300<\/span>  \n <span class=\"hljs-keyword\">NULL<\/span>    | San Jose      |            <span class=\"hljs-number\">150<\/span>  \n <span class=\"hljs-keyword\">NULL<\/span>    | Texas         |            <span class=\"hljs-number\">450<\/span>  \n <span class=\"hljs-keyword\">NULL<\/span>    | <span class=\"hljs-keyword\">NULL<\/span>          |            <span class=\"hljs-number\">900<\/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, the <code>CUBE<\/code> generates four grouping sets:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>(brand, warehouse)<\/code>: Calculate the total quantity by brand and warehouse.<\/li>\n\n\n\n<li><code>(brand, NULL)<\/code>: Calculate the subtotal per brand, summing all warehouses for that brand. <code>NULL<\/code> in the <code>warehouse<\/code> column.<\/li>\n\n\n\n<li><code>(NULL, warehouse)<\/code>: Calculate the subtotal per warehouse, summing across all brands. <code>NULL<\/code> in the <code>brand<\/code> column.<\/li>\n\n\n\n<li><code>(NULL, NULL)<\/code>: Calculate the grand total, summing all warehouses and brands together.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='differences-between-grouping-sets-rollup-and-cube'>Differences Between GROUPING SETS, ROLLUP, and CUBE <a href=\"#differences-between-grouping-sets-rollup-and-cube\" class=\"anchor\" id=\"differences-between-grouping-sets-rollup-and-cube\" title=\"Anchor for Differences Between GROUPING SETS, ROLLUP, and CUBE\">#<\/a><\/h2>\n\n\n\n<p>The following table explains the differences between <code>GROUPING SETS<\/code>, <code>ROLLUP<\/code> and <code>CUBE<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Option<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>CUBE<\/td><td>Generates <strong>all possible combinations<\/strong> of specified columns.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-rollup\/\">ROLLUP<\/a><\/td><td>Generates <strong>hierarchical summaries<\/strong>.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-grouping-sets\/\">GROUPING SETS<\/a><\/td><td>Generates <strong>specified grouping sets<\/strong> instead of generating all possibilities.<\/td><\/tr><\/tbody><\/table><\/figure>\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 PostgreSQL <code>CUBE<\/code> to create multi-dimensional summary by calculating all possible aggregations of the specified columns.<\/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=\"2114\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL CUBE\"\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=\"2114\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL CUBE\"\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 PostgreSQL CUBE to generate all possible aggregations of the specified columns. Introduction to PostgreSQL CUBE # The GROUP BY clause allows you to group rows and calculate an aggregation of a single grouping set. To calculate aggregations of all possible combinations of a set of columns, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":33,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2114","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 CUBE<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL CUBE to generate all possible aggregations of the specified columns.\" \/>\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-tutorial\/postgresql-cube\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL CUBE\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL CUBE to generate all possible aggregations of the specified columns.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-16T02:22:37+00:00\" \/>\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-tutorial\\\/postgresql-cube\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-cube\\\/\",\"name\":\"PostgreSQL CUBE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-02-16T02:21:16+00:00\",\"dateModified\":\"2025-02-16T02:22:37+00:00\",\"description\":\"In this tutorial, you'll learn how to use PostgreSQL CUBE to generate all possible aggregations of the specified columns.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-cube\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-cube\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-cube\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL CUBE\"}]},{\"@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 CUBE","description":"In this tutorial, you'll learn how to use PostgreSQL CUBE to generate all possible aggregations of the specified columns.","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-tutorial\/postgresql-cube\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL CUBE","og_description":"In this tutorial, you'll learn how to use PostgreSQL CUBE to generate all possible aggregations of the specified columns.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-02-16T02:22:37+00:00","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-tutorial\/postgresql-cube\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/","name":"PostgreSQL CUBE","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-02-16T02:21:16+00:00","dateModified":"2025-02-16T02:22:37+00:00","description":"In this tutorial, you'll learn how to use PostgreSQL CUBE to generate all possible aggregations of the specified columns.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-cube\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL CUBE"}]},{"@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\/2114","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=2114"}],"version-history":[{"count":5,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2114\/revisions"}],"predecessor-version":[{"id":2119,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2114\/revisions\/2119"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}