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

From a SELECT

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

Occasionally, creating a table from a SELECT statement is very useful, copying the source table's data types and data.

// Create a new table from a source SELECT statement
create.createTable("book_archive")
      .as(select(BOOK.ID, BOOK.TITLE).from(BOOK))
      .execute();

Dialect support

This example using jOOQ:

createTable("book_archive")
      .as(select(BOOK.ID, BOOK.TITLE).from(BOOK))

Translates to the following dialect specific expressions:

ASE, Access, SQLDataWarehouse, SQLServer

SELECT BOOK.ID, BOOK.TITLE
INTO book_archive
FROM BOOK

Aurora MySQL, Aurora Postgres, CockroachDB, Derby, DuckDB, Exasol, H2, MariaDB, MemSQL, MySQL, Oracle, Postgres, Redshift, SQLite, Snowflake, Vertica, YugabyteDB

CREATE TABLE book_archive
AS
SELECT BOOK.ID, BOOK.TITLE
FROM BOOK

DB2

BEGIN
  EXECUTE IMMEDIATE '
    CREATE TABLE book_archive
    AS (
      SELECT BOOK.ID, BOOK.TITLE
      FROM BOOK
    ) WITH NO DATA
  ';
  EXECUTE IMMEDIATE '
    INSERT INTO book_archive
    SELECT BOOK.ID, BOOK.TITLE
    FROM BOOK
  ';
END

Hana

CREATE TABLE book_archive
AS (
  SELECT BOOK.ID, BOOK.TITLE
  FROM BOOK
)

HSQLDB, Teradata

CREATE TABLE book_archive
AS (
  SELECT BOOK.ID, BOOK.TITLE
  FROM BOOK
)
WITH DATA

BigQuery, Firebird, Informix, Sybase, Trino

/* UNSUPPORTED */

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

References to this page

Feedback

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

The jOOQ Logo