---
title: grouping_id function
description: Learn the syntax of the grouping\_id function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/grouping_id
---

# `grouping_id` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the level of grouping for a set of columns.

## Syntax

```
grouping_id( [col1 [, ...] ] )
```

## Arguments

* `colN`: A column reference identified in a `GROUPING SET`, `ROLLUP`, or `CUBE`.

## Returns

A BIGINT.

The function combines the [`grouping` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/grouping.md) for several columns into one by assigning each column a bit in a bit vector.
The `col1` is represented by the highest order bit.
A bit is set to 1 if the row computes a subtotal for the corresponding column.

Specifying no argument is equivalent to specifying all columns listed in the `GROUPING SET`, `CUBE`, or `ROLLUP`.

## Examples

```sql
> SELECT name, age, grouping_id(name, age),
         conv(cast(grouping_id(name, age) AS STRING), 10, 2),
         avg(height)
    FROM VALUES (2, 'Alice', 165), (5, 'Bob', 180) people(age, name, height)
  GROUP BY cube(name, age)
Alice 2    0    0     165.0
Alice NULL 1    1     165.0
NULL  2    2   10     165.0
NULL  NULL 3   11     172.5
Bob   NULL 1    1     180.0
Bob   5    0    0     180.0
NULL  5    2   10     180.0
```

## Related functions

* [`grouping` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/grouping.md)
