Title: SQL CUBE: Every Grouping Combination in One Pass for…
Open Graph Title: SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports
X Title: SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports
Description: CUBE in PostgreSQL: every grouping combination in one query for cross-tab reports, reading NULL subtotals with GROUPING(), CUBE vs ROLLUP.
Open Graph Description: CUBE in PostgreSQL: every grouping combination in one query for cross-tab reports, reading NULL subtotals with GROUPING(), CUBE vs ROLLUP.
X Description: CUBE in PostgreSQL: every grouping combination in one query for cross-tab reports, reading NULL subtotals with GROUPING(), CUBE vs ROLLUP.
Keywords:
Opengraph URL: https://sql.coderang.dev/en/blog/sql-cube
Domain: sql.coderang.dev
IPv4 address: 79.137.205.149
{"@context":"https://schema.org","@type":"Organization","@id":"https://sql.coderang.dev/#organization","name":"SQL Arena","url":"https://sql.coderang.dev","logo":{"@type":"ImageObject","url":"https://sql.coderang.dev/android-chrome-192x192.png","width":192,"height":192},"sameAs":["https://t.me/coderang"],"description":"SQL Arena is an interactive SQL training platform — a practice arena, duels, a reference handbook and tutorials for analysts and backend developers."}
{"@context":"https://schema.org","@type":"WebSite","name":"SQL Arena","url":"https://sql.coderang.dev","inLanguage":"en-US","publisher":{"@id":"https://sql.coderang.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":"https://sql.coderang.dev/trainer/tasks?search={search_term_string}","query-input":"required name=search_term_string"}}
{"@context":"https://schema.org","@type":"TechArticle","headline":"SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports","description":"CUBE in PostgreSQL: every grouping combination in one query for cross-tab reports, reading NULL subtotals with GROUPING(), CUBE vs ROLLUP.","image":{"@type":"ImageObject","url":"https://sql.coderang.dev/og?lang=en&title=SQL%20CUBE%3A%20Every%20Grouping%20Combination%20in%20One%20Pass%20for%20Cross-Tab%20Reports","width":1200,"height":630},"datePublished":"2026-06-19T02:55:52.117Z","dateModified":"2026-06-26T02:27:05.000Z","author":{"@type":"Organization","@id":"https://sql.coderang.dev/#organization","name":"SQL Arena"},"publisher":{"@type":"Organization","@id":"https://sql.coderang.dev/#organization","name":"SQL Arena","url":"https://sql.coderang.dev","logo":{"@type":"ImageObject","url":"https://sql.coderang.dev/android-chrome-192x192.png","width":192,"height":192},"sameAs":["https://t.me/coderang"],"description":"SQL Arena is an interactive SQL training platform — a practice arena, duels, a reference handbook and tutorials for analysts and backend developers."},"mainEntityOfPage":{"@type":"WebPage","@id":"https://sql.coderang.dev/en/blog/sql-cube"},"inLanguage":"en-US","wordCount":554,"timeRequired":"PT3M","articleSection":"sql"}
{"@context":"https://schema.org","@type":"BreadcrumbList","inLanguage":"en-US","itemListElement":[{"@type":"ListItem","position":1,"name":"SQL Arena","item":"https://sql.coderang.dev/en"},{"@type":"ListItem","position":2,"name":"Blog","item":"https://sql.coderang.dev/en/blog"},{"@type":"ListItem","position":3,"name":"SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports","item":"https://sql.coderang.dev/en/blog/sql-cube"}]}
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en-US","mainEntity":[{"@type":"Question","name":"What CUBE computes","acceptedAnswer":{"@type":"Answer","text":"The canonical example: sales broken down by region and product. GROUP BY CUBE (region, product) builds four groupings at once: - (region, product) — the detail cells of the cross-tab; - (region) — a total per region (any product); - (product) — a total per product (any region); - () — the empty set, i.e. the grand total over all rows. For n columns, CUBE produces 2^n groupings. Two columns give 4 sets, three give 8, four give 16. That is the key difference from ROLLUP, which collapses columns right to left only (a hierarchy, n + 1 levels), while CUBE takes all subsets. CUBE (a, b) yields both (a) and (b); ROLLUP (a, b) yields (a) but not (b)."}},{"@type":"Question","name":"CUBE vs ROLLUP vs GROUPING SETS","acceptedAnswer":{"@type":"Answer","text":"All three are syntactic sugar over GROUPING SETS. Knowing the equivalents gives you full control: - CUBE (a, b) = GROUPING SETS ((a, b), (a), (b), ()) — every combination; - ROLLUP (a, b) = GROUPING SETS ((a, b), (a), ()) — only the right-to-left hierarchy; - need a non-standard set? Spell it out by hand: GROUPING SETS ((a, b), (b), ()). Reach for ROLLUP when the columns form a natural hierarchy (year then month then day) and you do not need side subtotals. Reach for CUBE when the dimensions are independent and you want the full matrix. Gotcha: CUBE over many columns explodes exponentially — CUBE over 5 columns is 32 groupings; on large tables that is expensive, so cap the number of dimensio"}}]}
| theme-color | #0a0a0a |
| application-name | SQL Arena |
| author | SQL Arena |
| creator | SQL Arena |
| publisher | SQL Arena |
| googlebot | index, follow, max-image-preview:large, max-snippet:-1 |
| google-site-verification | EaorZzgqTO4qTFsEEiRorcvcDZOqzyHpTwUJwsCjq_Q |
| og:site_name | SQL Arena |
| og:locale | en_US |
| og:image | https://sql.coderang.dev/og?lang=en&title=SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports |
| og:image:width | 1200 |
| og:image:height | 630 |
| og:image:alt | SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports |
| og:image:type | image/png |
| og:type | article |
| article:published_time | 2026-06-19T02:55:52.117Z |
| article:modified_time | 2026-06-26T02:27:05.000Z |
| article:tag | reporting |
| twitter:card | summary_large_image |
| twitter:image | https://sql.coderang.dev/og?lang=en&title=SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports |
| twitter:image:alt | SQL CUBE: Every Grouping Combination in One Pass for Cross-Tab Reports |
Links:
| Server | nginx |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | s-maxage=300, stale-while-revalidate |
Viewport: width=device-width, initial-scale=1, maximum-scale=5, viewport-fit=cover
Robots: index, follow