Solve “Deduplicate events with ROW_NUMBER”

Free. Runs in your browser, no install.

SQL · Aggregations & Grouping

What is CUBE?

Mediumcubegrouping sets

Interview question

What is CUBE?

How to answer

The short answer

CUBE generates aggregates for all combinations of the grouping columns.

For 3 columns:

Output
2³ = 8

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

For example:

SQL
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

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill