Available in versions: Dev (3.19) | Latest (3.18) | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10 | 3.9

GROUP BY empty grouping set

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

A special kind of GROUPING SET is the empty grouping set, which can be achieved in standard SQL and many SQL dialects using GROUP BY (). It is implicit, whenever an aggregate function is present in a query, but not an explicit GROUP BY clause.

SELECT COUNT(*)
FROM BOOK
GROUP BY ()
 
create.selectCount()
      .from(BOOK)
      .groupBy()
      .fetch();

Dialect support

This example using jOOQ:

selectCount().from(BOOK).groupBy()

Translates to the following dialect specific expressions:

-- ACCESS
SELECT count(*)
FROM BOOK, (select count(*) dual from MSysResources) as empty_grouping_dummy_table
GROUP BY empty_grouping_dummy_table.dual

-- ASE, BIGQUERY, SQLDATAWAREHOUSE
SELECT count(*)
FROM BOOK, (select 1 as dual) as empty_grouping_dummy_table
GROUP BY empty_grouping_dummy_table.dual

-- AURORA_MYSQL, MEMSQL
SELECT count(*)
FROM BOOK
GROUP BY (SELECT 1
FROM DUAL)

-- AURORA_POSTGRES, DB2, DUCKDB, EXASOL, H2, ORACLE, POSTGRES, SQLSERVER, SYBASE, TERADATA, TRINO
SELECT count(*)
FROM BOOK
GROUP BY ()

-- COCKROACHDB, MARIADB, MYSQL, REDSHIFT, SQLITE, VERTICA, YUGABYTEDB
SELECT count(*)
FROM BOOK
GROUP BY (SELECT 1)

-- DERBY, HSQLDB
SELECT count(*)
FROM BOOK
GROUP BY 0

-- FIREBIRD
SELECT count(*)
FROM BOOK
GROUP BY (SELECT 1
FROM RDB$DATABASE)

-- HANA, SNOWFLAKE
SELECT count(*)
FROM BOOK
GROUP BY GROUPING SETS (())

-- INFORMIX
SELECT count(*)
FROM BOOK, (select 1 as dual from systables where tabid = 1) as empty_grouping_dummy_table
GROUP BY empty_grouping_dummy_table.dual

(These are currently generated with jOOQ 3.19, see #10141), or translate your own on our website

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo