Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. What is CUBE?

SQL · Aggregations & Grouping

What is CUBE?

Mediumsql-15
cubegrouping sets

Question

What is CUBE?

Solution

CUBE generates aggregates for all combinations of the grouping columns.

For 3 columns:

2³ = 8

So CUBE(a,b,c) produces up to 8 grouping combinations.

For example:

GROUP BY CUBE(country, city, product)

can produce totals for:

  • country + city + product
  • country + city
  • country + product
  • city + product
  • country
  • city
  • product
  • grand total

ROLLUP = hierarchical combinations

CUBE = all combinations

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext