---
title: "What is CUBE in SQL Server?"  
description: "What is CUBE in SQL Server?"  
author: "Anubhav Sharma"  
published: 2026-04-19  
updated: 2026-04-19  
canonical: https://www.mindstick.com/interview/34488/what-is-cube-in-sql-server  
category: "SQL Server"  
tags: ["sql server", "sql server 2022"]  
reading_time: 4 minutes  

---

# What is CUBE in SQL Server?

In SQL Server, **CUBE** is used in `GROUP BY` to generate **all possible combinations of aggregations** across multiple columns.

## Simple Meaning

> ## CUBE = multi-dimensional totals (all combinations)

## Syntax

```plaintext
SELECT column1, column2, SUM(amount)
FROM table
GROUP BY CUBE (column1, column2);
```

## Example

Suppose you have sales data:

| Region | Product | Amount |
| --- | --- | --- |
| North | A | 100 |
| North | B | 200 |
| South | A | 150 |

### Query:

```plaintext
SELECT Region, Product, SUM(Amount) AS Total
FROM Sales
GROUP BY CUBE (Region, Product);
```

## Output Explained

CUBE will generate:

- Region + Product (normal grouping)
- Region total (Product = NULL)
- Product total (Region = NULL)
- Grand total (both NULL)

### Result:

| Region | Product | Total |
| --- | --- | --- |
| North | A | 100 |
| North | B | 200 |
| South | A | 150 |
| North | NULL | 300 |
| South | NULL | 150 |
| NULL | A | 250 |
| NULL | B | 200 |
| NULL | NULL | 450 |

## Key Point

- `NULL` means **total (aggregation)**
- CUBE creates **2ⁿ combinations**

   - 2 columns → 4 combinations
   - 3 columns → 8 combinations

## Query Using CUBE

```plaintext
SELECT Region, Product, SUM(Amount) AS Total
FROM Sales
GROUP BY CUBE (Region, Product);
```

## Understanding

- `(Region, Product)` → Normal grouping
- `(Region, NULL)` → Total per Region
- `(NULL, Product)` → Total per Product
- `(NULL, NULL)` → Overall total

##

## Difference from ROLLUP

- **CUBE** → all combinations
- **ROLLUP** → hierarchical totals only

## When to Use

Use CUBE when you need:

- Full reports
- Pivot-style summaries
- Multi-dimensional analysis

## One-Line Summary

> ## CUBE = automatic subtotal + grand total for every column combination

## Answers

### Answer by Anubhav Sharma

In SQL Server, **CUBE** is used in `GROUP BY` to generate **all possible combinations of aggregations** across multiple columns.

## Simple Meaning

> ## CUBE = multi-dimensional totals (all combinations)

## Syntax

```plaintext
SELECT column1, column2, SUM(amount)
FROM table
GROUP BY CUBE (column1, column2);
```

## Example

Suppose you have sales data:

| Region | Product | Amount |
| --- | --- | --- |
| North | A | 100 |
| North | B | 200 |
| South | A | 150 |

### Query:

```plaintext
SELECT Region, Product, SUM(Amount) AS Total
FROM Sales
GROUP BY CUBE (Region, Product);
```

## Output Explained

CUBE will generate:

- Region + Product (normal grouping)
- Region total (Product = NULL)
- Product total (Region = NULL)
- Grand total (both NULL)

### Result:

| Region | Product | Total |
| --- | --- | --- |
| North | A | 100 |
| North | B | 200 |
| South | A | 150 |
| North | NULL | 300 |
| South | NULL | 150 |
| NULL | A | 250 |
| NULL | B | 200 |
| NULL | NULL | 450 |

## Key Point

- `NULL` means **total (aggregation)**
- CUBE creates **2ⁿ combinations**

   - 2 columns → 4 combinations
   - 3 columns → 8 combinations

## Query Using CUBE

```plaintext
SELECT Region, Product, SUM(Amount) AS Total
FROM Sales
GROUP BY CUBE (Region, Product);
```

## Understanding

- `(Region, Product)` → Normal grouping
- `(Region, NULL)` → Total per Region
- `(NULL, Product)` → Total per Product
- `(NULL, NULL)` → Overall total

##

## Difference from ROLLUP

- **CUBE** → all combinations
- **ROLLUP** → hierarchical totals only

## When to Use

Use CUBE when you need:

- Full reports
- Pivot-style summaries
- Multi-dimensional analysis

## One-Line Summary

> ## CUBE = automatic subtotal + grand total for every column combination


---

Original Source: https://www.mindstick.com/interview/34488/what-is-cube-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
