CUBE Concept
CUBE Concept
CUBE: Every Possible Combination
CUBE is the most exhaustive member of the GROUPING SETS family (Topic 15.6). While ROLLUP only produces a hierarchical, right-to-left nested sequence of subtotals (Topic 15.7), CUBE(col1, col2, ...) produces every possible combination of the listed columns — a full power set of grouping levels.
CUBE(region, product) expands to GROUPING SETS ((region, product), (region), (product), ()) — all four possible combinations of "include region," "include product."
Running Example
All examples in this chapter use one small orders table:
sqlCREATE TABLE orders ( id INT PRIMARY KEY, region VARCHAR(10), product VARCHAR(10), amount NUMERIC(8,2) );
| id | region | product | amount |
|---|---|---|---|
| 1 | East | Widget | 100 |
| 2 | East | Gadget | 150 |
| 3 | West | Widget | 200 |
| 4 | West | Widget | 50 |
| 5 | West | Gadget | 300 |
| 6 | North | Widget | 120 |
| 7 | North | Gadget | 80 |
| 8 | East | Widget | 60 |
Total of all 8 rows: 1060. Per region: East = 310, West = 550, North = 200. Per product: Widget = 530, Gadget = 530 (100+200+50+120+60 and 150+300+80).
Example
sqlSELECT region, product, SUM(amount) AS total_amount FROM orders GROUP BY CUBE(region, product) ORDER BY region, product;
Result (12 rows: 6 detail + 3 region subtotals + 2 product subtotals + 1 grand total):
| region | product | total_amount |
|---|---|---|
| East | Gadget | 150 |
| East | Widget | 160 |
| East | NULL | 310 |
| North | Gadget | 80 |
| North | Widget | 120 |
| North | NULL | 200 |
| West | Gadget | 300 |
| West | Widget | 250 |
| West | NULL | 550 |
| NULL | Gadget | 530 |
| NULL | Widget | 530 |
| NULL | NULL | 1060 |
Compared to ROLLUP(region, product)'s 10 rows, CUBE adds the two extra product-only subtotal rows (region=NULL, product='Gadget' = 530 and region=NULL, product='Widget' = 530) that ROLLUP never produces.
CUBE vs ROLLUP, Directly Compared
| ROLLUP(region, product) | CUBE(region, product) | |
|---|---|---|
| Detail rows (region, product) | Yes (6) | Yes (6) |
| Region-only subtotals | Yes (3) | Yes (3) |
| Product-only subtotals | No | Yes (2) |
| Grand total | Yes (1) | Yes (1) |
| Total rows | 10 | 12 |
ROLLUP assumes a hierarchy (drop columns right-to-left only); CUBE makes no such assumption and computes every subset, so it grows combinatorially: with n grouping columns, CUBE produces 2^n grouping sets, versus ROLLUP's n + 1.
Why This Beats Manual Queries
Getting this same 12-row result manually would require four separate GROUP BY queries ((region, product), (region), (product), and none) combined with UNION ALL — four full scans of orders instead of one.
Dialect Support (Important Caveat)
Like GROUPING SETS, CUBE is supported in PostgreSQL, SQL Server, and Oracle. MySQL does not support `CUBE` at all (as of mainstream MySQL 8.x) — only the simpler WITH ROLLUP modifier exists there. On MySQL, replicating CUBE's output requires manually writing out all 2^n GROUP BY/UNION ALL combinations.
Edge Cases
CUBEwith 3+ columns grows fast:CUBE(a, b, c)produces2^3 = 8grouping sets — every subset from full detail down to the grand total.- As with
ROLLUP/GROUPING SETS, useGROUPING(column)to tell a real storedNULLapart from a subtotal-markerNULL. CUBEandROLLUPcan technically be combined or mixed with explicitGROUPING SETSentries in the sameGROUP BYclause in dialects that support it (PostgreSQL, SQL Server), for custom combinations.
Key Takeaways / Interview Q&A
- Q: What does CUBE(region, product) compute that ROLLUP(region, product) does not?
A: The two product-only subtotal rows (Widget=530, Gadget=530 across all regions) — CUBE computes every combination, not just a hierarchical rollup.
- Q: How many grouping sets does CUBE(a, b, c) produce?
A: 2^3 = 8 (every subset of {a, b, c}, including the empty set).
- Q: How many rows does GROUP BY CUBE(region, product) return here?
A: 12 — 6 detail + 3 region subtotals + 2 product subtotals + 1 grand total.
- Q: Does MySQL support CUBE?
A: No — only PostgreSQL, SQL Server, and Oracle support CUBE; MySQL has neither CUBE nor full GROUPING SETS, only WITH ROLLUP.