Back to Yugabyte Db

CUBE

community/learn/content/01-SQL-Basics/02-fundamentals/17-cube.md

2026.1.0.0-b25705 B
Original Source

SELECT with a GROUP BY and CUBE

In this exercise we will query the products table, group the results and then generate multiple grouping sets from the results.

SELECT product_id, supplier_id, product_name, 
       SUM(units_in_stock)
FROM products
GROUP BY product_id, CUBE(supplier_id);

This query should return 154 rows

SELECT with a GROUP BY and a partial CUBE

In this exercise we will query the products table, group the results and then generate a subset of grouping sets from the results.

SELECT product_id, supplier_id, product_name, 
       SUM(units_in_stock)
FROM products
GROUP BY product_id, CUBE(product_id, supplier_id);

This query should return 200 rows