CUBE

SQL · Grouping Rows

Theory

CUBE produces subtotals for every possible combination of the listed columns. GROUP BY CUBE (dept, role) creates the groupings (dept, role), (dept), (role) and (): details, a subtotal per department, a subtotal per role across all departments and a grand total. With n columns it creates 2 to the power n groupings.

Compared with ROLLUP, CUBE does not assume a hierarchy, so it also gives cross-cutting totals like "all Leads in every department". That makes it useful for multi-dimensional analysis and pivot-style reports, but the result grows fast: three columns already give eight grouping levels. As with ROLLUP, subtotal rows show NULL, and GROUPING(col) tells them apart from real NULLs. PostgreSQL, SQL Server, Oracle and Snowflake support CUBE; MySQL and SQLite do not.

Why we use it

Analysts often want to slice totals by each dimension on its own and in combination. CUBE delivers every slice in one query, ready for a pivot table or dashboard.

When to use it

Use CUBE when you need totals for all combinations of a few dimensions. Use ROLLUP for a single hierarchy, or GROUPING SETS when you only need a few specific levels, to keep the result small.

How it works

  1. GROUP BY CUBE (dept, role)
  2. List all combinations: (dept, role), (dept), (role), ()
  3. Aggregate rows for each combination
  4. Fill columns not in the combination with NULL
  5. More than 3 columns? Yes: Very large result: consider GROUPING SETS. No: Size stays manageable.
  6. Return all subtotal levels together

Syntax

SELECT c1, c2, AGG(c3)
FROM table
GROUP BY CUBE (c1, c2);

Example

SELECT department, role, COUNT(*)
FROM employees
GROUP BY CUBE (department, role);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic