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

SQL building

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

SQL is a declarative language that is hard to integrate into procedural, object-oriented, functional or any other type of programming languages. jOOQ's philosophy is to give SQL the credit it deserves and integrate SQL itself as an "internal domain specific language" directly into Java.

With this philosophy in mind, SQL building is the main feature of jOOQ. All other features (such as SQL execution and code generation) are mere convenience built on top of jOOQ's SQL building capabilities.

This section explains all about the various syntax elements involved with jOOQ's SQL building capabilities. For a complete overview of all syntax elements, please refer to the manual's sections about SQL to DSL mapping rules.

Table of contents

4.1.
The query DSL type
4.1.1.
DSL subclasses
4.2.
The DSLContext API
4.2.1.
SQL Dialect
4.2.2.
SQL Dialect Family
4.2.3.
Connection vs. DataSource
4.2.4.
Custom data
4.2.5.
Custom ExecuteListeners
4.2.6.
Custom Unwrappers
4.2.7.
Custom Settings
4.2.7.1.
Auto-attach Records
4.2.7.2.
Backslash Escaping
4.2.7.3.
Batch size
4.2.7.4.
Execute Logging
4.2.7.5.
Fetch Warnings
4.2.7.6.
GROUP_CONCAT Configuration (new)
4.2.7.7.
Identifier style
4.2.7.8.
Implicit join type
4.2.7.9.
Inline Threshold
4.2.7.10.
IN-list Padding
4.2.7.11.
Interpreter Configuration
4.2.7.12.
JDBC Flags
4.2.7.13.
Keyword style
4.2.7.14.
Listener Invocation Order
4.2.7.15.
Locales
4.2.7.16.
Map JPA Annotations
4.2.7.17.
Object qualification
4.2.7.18.
Optimistic Locking
4.2.7.19.
Parameter name prefix
4.2.7.20.
Parameter types
4.2.7.21.
Parser Configuration
4.2.7.22.
Reflection caching
4.2.7.23.
Return all columns on store
4.2.7.24.
Return Identity Value On Store
4.2.7.25.
Runtime catalog, schema and table mapping
4.2.7.26.
Scalar subqueries for stored functions
4.2.7.27.
Statement Type
4.2.7.28.
Updatable Primary Keys
4.2.8.
Thread safety
4.3.
SQL Statements (DML)
4.3.1.
jOOQ's DSL and model API
4.3.2.
The WITH clause
4.3.3.
The WITH RECURSIVE clause
4.3.4.
The SELECT statement
4.3.4.1.
SELECT clause
4.3.4.1.1.
Projection type safety
4.3.4.1.2.
SelectField
4.3.4.1.3.
SELECT *
4.3.4.1.4.
SELECT * EXCEPT (...)
4.3.4.1.5.
SELECT DISTINCT
4.3.4.1.6.
SELECT DISTINCT ON
4.3.4.1.7.
Convenience methods
4.3.4.2.
FROM clause
4.3.4.3.
JOIN operator
4.3.4.4.
Implicit path JOIN
4.3.4.5.
WHERE clause
4.3.4.6.
CONNECT BY clause
4.3.4.7.
GROUP BY clause
4.3.4.7.1.
GROUP BY columns
4.3.4.7.2.
GROUP BY ROLLUP
4.3.4.7.3.
GROUP BY CUBE
4.3.4.7.4.
GROUP BY GROUPING SETS
4.3.4.7.5.
GROUP BY empty grouping set
4.3.4.8.
HAVING clause
4.3.4.9.
WINDOW clause
4.3.4.10.
QUALIFY clause
4.3.4.11.
ORDER BY clause
4.3.4.12.
LIMIT .. OFFSET clause
4.3.4.13.
WITH TIES clause
4.3.4.14.
SEEK clause
4.3.4.15.
FOR clause
4.3.4.16.
FOR UPDATE clause
4.3.4.17.
Set operations
4.3.4.17.1.
Type safety
4.3.4.17.2.
Projection rowtype
4.3.4.17.3.
Differences to standard SQL
4.3.4.17.4.
UNION
4.3.4.17.5.
INTERSECT
4.3.4.17.6.
EXCEPT
4.3.4.18.
Lexical and logical SELECT clause order
4.3.5.
The INSERT statement
4.3.5.1.
INSERT .. VALUES
4.3.5.2.
INSERT .. DEFAULT VALUES
4.3.5.3.
INSERT .. SET
4.3.5.4.
INSERT .. SELECT
4.3.5.5.
INSERT .. ON DUPLICATE KEY UPDATE
4.3.5.6.
INSERT .. ON DUPLICATE KEY IGNORE
4.3.5.7.
INSERT .. ON CONFLICT
4.3.5.8.
INSERT .. RETURNING
4.3.6.
The UPDATE statement
4.3.6.1.
UPDATE .. SET
4.3.6.2.
UPDATE .. SET ROWS
4.3.6.3.
UPDATE .. FROM
4.3.6.4.
UPDATE .. WHERE
4.3.6.5.
UPDATE .. ORDER BY .. LIMIT
4.3.6.6.
UPDATE .. RETURNING
4.3.7.
The DELETE statement
4.3.7.1.
DELETE .. USING
4.3.7.2.
DELETE .. WHERE
4.3.7.3.
DELETE .. ORDER BY .. LIMIT
4.3.7.4.
DELETE .. RETURNING
4.3.8.
The MERGE statement
4.4.
SQL Statements (DDL)
4.4.1.
The ALTER statement
4.4.1.1.
ALTER DATABASE
4.4.1.2.
ALTER DOMAIN
4.4.1.3.
ALTER INDEX
4.4.1.4.
ALTER SCHEMA
4.4.1.5.
ALTER SEQUENCE
4.4.1.6.
ALTER TABLE
4.4.1.7.
ALTER TYPE
4.4.1.7.1.
ALTER TYPE .. RENAME
4.4.1.7.2.
ALTER TYPE .. for enum alterations
4.4.1.8.
ALTER VIEW
4.4.1.8.1.
ALTER VIEW .. COMMENT
4.4.1.8.2.
ALTER VIEW .. RENAME
4.4.1.8.3.
ALTER VIEW IF EXISTS
4.4.2.
The COMMENT statement
4.4.2.1.
COMMENT ON TABLE
4.4.2.2.
COMMENT ON VIEW
4.4.2.3.
COMMENT ON COLUMN
4.4.3.
The CREATE statement
4.4.3.1.
CREATE DATABASE
4.4.3.2.
CREATE DOMAIN
4.4.3.3.
CREATE FUNCTION (new)
4.4.3.3.1.
Scalar functions (new)
4.4.3.3.2.
CREATE OR REPLACE FUNCTION (new)
4.4.3.3.3.
SQL data access characteristics (new)
4.4.3.3.4.
DETERMINISTIC characteristic (new)
4.4.3.3.5.
ON NULL INPUT characteristic (new)
4.4.3.4.
CREATE INDEX
4.4.3.5.
CREATE PROCEDURE (new)
4.4.3.5.1.
CREATE OR REPLACE PROCEDURE (new)
4.4.3.5.2.
SQL data access characteristics (new)
4.4.3.6.
CREATE SCHEMA
4.4.3.7.
CREATE SEQUENCE
4.4.3.8.
CREATE TABLE
4.4.3.8.1.
Columns
4.4.3.8.2.
Nullability
4.4.3.8.3.
Defaults
4.4.3.8.4.
Identities
4.4.3.8.5.
Primary key
4.4.3.8.6.
Unique constraints
4.4.3.8.7.
Foreign keys
4.4.3.8.8.
Check constraints
4.4.3.8.9.
From a SELECT
4.4.3.8.10.
Temporary tables
4.4.3.9.
CREATE TRIGGER (new)
4.4.3.9.1.
Events (new)
4.4.3.9.2.
REFERENCING clause (new)
4.4.3.9.3.
STATEMENT vs ROW triggers (new)
4.4.3.9.4.
WHEN clause (new)
4.4.3.10.
CREATE TYPE
4.4.3.11.
CREATE VIEW
4.4.3.11.1.
OR REPLACE
4.4.3.11.2.
WITH CHECK OPTION
4.4.3.11.3.
WITH READ ONLY
4.4.4.
The DROP statement
4.4.4.1.
DROP DATABASE
4.4.4.1.1.
IF EXISTS
4.4.4.2.
DROP DOMAIN
4.4.4.2.1.
IF EXISTS
4.4.4.3.
DROP FUNCTION (new)
4.4.4.3.1.
IF EXISTS (new)
4.4.4.4.
DROP INDEX
4.4.4.4.1.
IF EXISTS
4.4.4.5.
DROP PROCEDURE (new)
4.4.4.5.1.
IF EXISTS (new)
4.4.4.6.
DROP SCHEMA
4.4.4.6.1.
IF EXISTS
4.4.4.7.
DROP SEQUENCE
4.4.4.7.1.
IF EXISTS
4.4.4.8.
DROP TABLE
4.4.4.8.1.
IF EXISTS
4.4.4.9.
DROP TRIGGER (new)
4.4.4.9.1.
IF EXISTS (new)
4.4.4.10.
DROP TYPE
4.4.4.10.1.
IF EXISTS
4.4.4.11.
DROP VIEW
4.4.4.11.1.
IF EXISTS
4.4.5.
The GRANT statement
4.4.6.
The REVOKE statement
4.4.7.
The SET statement
4.4.7.1.
SET CATALOG
4.4.7.2.
SET SCHEMA
4.4.8.
The TRUNCATE statement
4.4.9.
Generating DDL from objects
4.5.
Procedural statements
4.5.1.
Block statement
4.5.2.
CALL statement (new)
4.5.3.
CONTINUE statement
4.5.4.
EXECUTE statement
4.5.5.
EXIT statement
4.5.6.
FOR statement
4.5.7.
GOTO statement
4.5.8.
IF statement
4.5.9.
Labels
4.5.10.
LOOP statement
4.5.11.
REPEAT statement
4.5.12.
SIGNAL (new)
4.5.13.
Variables
4.5.14.
WHILE statement
4.6.
Catalog and schema expressions
4.7.
Table expressions
4.7.1.
Generated Tables
4.7.2.
Aliased Tables
4.7.2.1.
Aliased generated tables
4.7.2.2.
Aliased table expressions
4.7.2.3.
Derived column lists
4.7.2.4.
Unnamed derived tables
4.7.3.
Joined tables
4.7.3.1.
CROSS JOIN
4.7.3.2.
INNER JOIN
4.7.3.3.
OUTER JOIN
4.7.3.4.
SEMI JOIN
4.7.3.5.
ANTI JOIN
4.7.3.6.
ON clause
4.7.3.7.
ON KEY clause
4.7.3.8.
USING clause
4.7.3.9.
NATURAL clause
4.7.3.10.
LATERAL
4.7.3.11.
APPLY
4.7.3.12.
PARTITION BY
4.7.4.
The VALUES() table constructor
4.7.5.
Derived tables
4.7.6.
Inline derived tables
4.7.7.
The Oracle 11g PIVOT clause
4.7.8.
jOOQ's relational division syntax
4.7.9.
Array and cursor unnesting
4.7.10.
Table-valued functions
4.7.11.
GENERATE_SERIES
4.7.12.
JSON_TABLE
4.7.13.
XMLTABLE
4.7.14.
The DUAL table
4.7.15.
Temporal tables
4.8.
Column expressions
4.8.1.
Table columns
4.8.1.1.
Generated table columns
4.8.1.2.
Dereferenced table columns
4.8.1.3.
Named table columns
4.8.2.
Aliased columns
4.8.3.
Cast expressions
4.8.4.
Datatype coercions
4.8.5.
Collations
4.8.6.
Arithmetic expressions
4.8.7.
String concatenation
4.8.8.
Case sensitivity with strings
4.8.9.
General functions
4.8.9.1.
CHOOSE
4.8.9.2.
COALESCE
4.8.9.3.
DECODE
4.8.9.4.
IIF
4.8.9.5.
NULLIF
4.8.9.6.
NVL
4.8.9.7.
NVL2
4.8.10.
Numeric functions
4.8.10.1.
ABS
4.8.10.2.
ACOS
4.8.10.3.
ASIN
4.8.10.4.
ATAN
4.8.10.5.
ATAN2
4.8.10.6.
CEIL
4.8.10.7.
COS
4.8.10.8.
COSH
4.8.10.9.
COT
4.8.10.10.
COTH
4.8.10.11.
DEG
4.8.10.12.
E
4.8.10.13.
EXP
4.8.10.14.
FLOOR
4.8.10.15.
GREATEST
4.8.10.16.
LEAST
4.8.10.17.
LN
4.8.10.18.
LOG
4.8.10.19.
LOG10 (new)
4.8.10.20.
NEG
4.8.10.21.
PI
4.8.10.22.
POWER
4.8.10.23.
RAD
4.8.10.24.
RAND
4.8.10.25.
ROUND
4.8.10.26.
SIGN
4.8.10.27.
SIN
4.8.10.28.
SINH
4.8.10.29.
SQRT
4.8.10.30.
SQUARE (new)
4.8.10.31.
TAN
4.8.10.32.
TANH
4.8.10.33.
TRUNC
4.8.10.34.
WIDTH_BUCKET
4.8.11.
Bitwise functions
4.8.11.1.
BIT_AND
4.8.11.2.
BIT_COUNT
4.8.11.3.
BIT_NAND
4.8.11.4.
BIT_NOR
4.8.11.5.
BIT_NOT
4.8.11.6.
BIT_OR
4.8.11.7.
BIT_XNOR
4.8.11.8.
BIT_XOR
4.8.11.9.
SHL
4.8.11.10.
SHR
4.8.12.
String functions
4.8.12.1.
ASCII
4.8.12.2.
BIT_LENGTH
4.8.12.3.
CHR (new)
4.8.12.4.
CONCAT (|| operator)
4.8.12.5.
DIGITS (new)
4.8.12.6.
LEFT
4.8.12.7.
LENGTH
4.8.12.8.
LOWER
4.8.12.9.
LPAD
4.8.12.10.
LTRIM
4.8.12.11.
MD5
4.8.12.12.
MID
4.8.12.13.
OCTET_LENGTH
4.8.12.14.
OVERLAY
4.8.12.15.
POSITION
4.8.12.16.
REGEXP_REPLACE
4.8.12.17.
REPEAT
4.8.12.18.
REPLACE
4.8.12.19.
REVERSE
4.8.12.20.
RIGHT
4.8.12.21.
RPAD
4.8.12.22.
RTRIM
4.8.12.23.
SPACE
4.8.12.24.
SPLIT_PART (new)
4.8.12.25.
SUBSTRING
4.8.12.26.
SUBSTRING_INDEX (new)
4.8.12.27.
TO_CHAR
4.8.12.28.
TO_HEX (new)
4.8.12.29.
TRANSLATE
4.8.12.30.
TRIM
4.8.12.31.
UPPER
4.8.12.32.
UUID (new)
4.8.13.
Datetime functions
4.8.13.1.
CENTURY
4.8.13.2.
CURRENT_DATE
4.8.13.3.
CURRENT_LOCALDATE
4.8.13.4.
CURRENT_LOCALDATETIME
4.8.13.5.
CURRENT_LOCALTIME
4.8.13.6.
CURRENT_OFFSETDATETIME
4.8.13.7.
CURRENT_OFFSETTIME
4.8.13.8.
CURRENT_TIME
4.8.13.9.
CURRENT_TIMESTAMP
4.8.13.10.
DATE
4.8.13.11.
DATEADD
4.8.13.12.
DATEDIFF
4.8.13.13.
DATESUB
4.8.13.14.
DAY
4.8.13.15.
DAY_OF_YEAR
4.8.13.16.
DECADE
4.8.13.17.
EPOCH
4.8.13.18.
EXTRACT
4.8.13.19.
HOUR
4.8.13.20.
ISO_DAY_OF_WEEK
4.8.13.21.
LOCALDATE
4.8.13.22.
LOCALDATEADD
4.8.13.23.
LOCALDATESUB
4.8.13.24.
LOCALDATETIME
4.8.13.25.
LOCALDATETIMEADD
4.8.13.26.
LOCALDATETIMESUB
4.8.13.27.
LOCALTIME
4.8.13.28.
MILLENNIUM
4.8.13.29.
MINUTE
4.8.13.30.
MONTH
4.8.13.31.
QUARTER
4.8.13.32.
SECOND
4.8.13.33.
TIME
4.8.13.34.
TIMESTAMP
4.8.13.35.
TIMESTAMPADD
4.8.13.36.
TIMESTAMPSUB
4.8.13.37.
TO_DATE
4.8.13.38.
TO_LOCALDATE
4.8.13.39.
TO_LOCALDATETIME
4.8.13.40.
TO_TIMESTAMP
4.8.13.41.
TRUNC
4.8.13.42.
YEAR
4.8.14.
ARRAY functions
4.8.14.1.
ARRAY_GET
4.8.14.2.
ARRAY constructor
4.8.14.3.
ARRAY constructor from subquery (new)
4.8.14.4.
CARDINALITY
4.8.15.
JSON functions
4.8.15.1.
JSON_ARRAY
4.8.15.2.
JSON_OBJECT
4.8.15.3.
JSON_VALUE
4.8.16.
XML functions
4.8.16.1.
XMLATTRIBUTES
4.8.16.2.
XMLCOMMENT
4.8.16.3.
XMLCONCAT
4.8.16.4.
XMLDOCUMENT
4.8.16.5.
XMLELEMENT
4.8.16.6.
XMLFOREST
4.8.16.7.
XMLPARSE
4.8.16.8.
XMLPI
4.8.16.9.
XMLQUERY
4.8.16.10.
XMLSERIALIZE (new)
4.8.17.
System functions
4.8.17.1.
CURRENT_SCHEMA
4.8.17.2.
CURRENT_USER
4.8.18.
Aggregate functions
4.8.18.1.
Grouping
4.8.18.2.
Distinctness
4.8.18.3.
Filtering
4.8.18.4.
Ordering
4.8.18.5.
Ordering WITHIN GROUP
4.8.18.6.
Keeping
4.8.18.7.
ANY_VALUE (new)
4.8.18.8.
ARRAY_AGG
4.8.18.9.
AVG
4.8.18.10.
BOOL_AND
4.8.18.11.
BOOL_OR
4.8.18.12.
COLLECT
4.8.18.13.
COUNT
4.8.18.14.
CUME_DIST
4.8.18.15.
DENSE_RANK
4.8.18.16.
EVERY
4.8.18.17.
GROUP_CONCAT
4.8.18.18.
JSON_ARRAYAGG
4.8.18.19.
JSON_OBJECTAGG
4.8.18.20.
LISTAGG
4.8.18.21.
MAX
4.8.18.22.
MEDIAN
4.8.18.23.
MIN
4.8.18.24.
MODE
4.8.18.25.
MULTISET_AGG (new)
4.8.18.26.
PERCENT_RANK
4.8.18.27.
PERCENTILE_CONT
4.8.18.28.
PERCENTILE_DISC
4.8.18.29.
PRODUCT
4.8.18.30.
RANK
4.8.18.31.
SUM
4.8.18.32.
XMLAGG
4.8.19.
Window functions
4.8.19.1.
PARTITION BY
4.8.19.2.
ORDER BY
4.8.19.3.
ROWS, RANGE, GROUPS (frame clause)
4.8.19.4.
EXCLUDE
4.8.19.5.
NULL treatment
4.8.19.6.
FROM FIRST, FROM LAST
4.8.19.7.
Nested aggregate functions
4.8.19.8.
Window aggregation
4.8.19.9.
Window ordered aggregate
4.8.19.10.
ROW_NUMBER
4.8.19.11.
RANK
4.8.19.12.
DENSE_RANK
4.8.19.13.
PERCENT_RANK
4.8.19.14.
CUME_DIST
4.8.19.15.
NTILE
4.8.19.16.
LEAD
4.8.19.17.
LAG
4.8.19.18.
FIRST_VALUE
4.8.19.19.
LAST_VALUE
4.8.19.20.
NTH_VALUE
4.8.20.
Grouping functions
4.8.21.
User-defined functions
4.8.22.
User-defined aggregate functions
4.8.23.
The CASE expression
4.8.24.
Sequences and serials
4.8.25.
Scalar subqueries
4.8.26.
ARRAY value constructor (new)
4.8.27.
MULTISET value constructor (new)
4.8.28.
Tuples or row value expressions
4.8.29.
Nested records (new)
4.9.
Conditional expressions
4.9.1.
Condition building
4.9.2.
TRUE and FALSE condition
4.9.3.
BOOLEAN columns
4.9.4.
AND, OR, NOT boolean operators
4.9.5.
Comparison predicate
4.9.6.
Boolean operator precedence
4.9.7.
Comparison predicate (degree > 1)
4.9.8.
Quantified comparison predicate
4.9.9.
BETWEEN predicate
4.9.10.
BETWEEN predicate (degree > 1)
4.9.11.
DISTINCT predicate
4.9.12.
DOCUMENT predicate
4.9.13.
EXISTS predicate
4.9.14.
IN predicate
4.9.15.
IN predicate (degree > 1)
4.9.16.
JSON predicate
4.9.17.
JSON_EXISTS predicate
4.9.18.
LIKE predicate
4.9.19.
NULL predicate
4.9.20.
NULL predicate (degree > 1)
4.9.21.
OVERLAPS predicate
4.9.22.
SIMILAR TO predicate
4.9.23.
UNIQUE predicate
4.9.24.
XMLEXISTS predicate
4.9.25.
Query By Example (QBE)
4.10.
Operator precedence
4.11.
Synthetic SQL clauses
4.12.
Dynamic SQL
4.12.1.
Optional conditional expressions
4.13.
Plain SQL
4.14.
Plain SQL Templating Language
4.15.
Hints
4.15.1.
MySQL hints
4.15.1.1.
Index hints
4.15.1.2.
STRAIGHT_JOIN
4.15.1.3.
Oracle style hints in MySQL
4.15.2.
Oracle hints
4.15.3.
SQL Server hints
4.15.3.1.
WITH
4.15.3.2.
OPTION
4.16.
SQL Parser
4.16.1.
SQL Parser API
4.16.2.
SQL Parser CLI
4.16.3.
SQL Parser Listener (new)
4.16.4.
SQL translator
4.16.5.
SQL Parser Grammar
4.17.
SQL interpreter
4.18.
Schema diff
4.19.
Schema diff CLI
4.20.
Names and identifiers
4.21.
Bind values and parameters
4.21.1.
Indexed parameters
4.21.2.
Named parameters
4.21.3.
Inlined parameters
4.21.4.
SQL injection
4.22.
QueryParts
4.22.1.
SQL rendering
4.22.2.
Declaration vs reference
4.22.3.
Pretty printing SQL
4.22.4.
Variable binding
4.22.5.
Custom data type bindings
4.22.6.
Custom syntax elements
4.22.7.
Plain SQL QueryParts
4.22.8.
Serializability
4.22.9.
SQL transformation
4.22.9.1.
ANSI JOIN to table lists
4.22.9.2.
Table lists to ANSI JOIN
4.22.9.3.
ROWNUM to LIMIT
4.22.9.4.
QUALIFY to derived table (new)
4.22.9.5.
IN condition subquery with LIMIT to derived table (new)
4.22.9.6.
Unnecessary arithmetic expressions
4.22.10.
Custom SQL transformation with VisitListener
4.22.10.1.
Example: Logging abbreviated bind values
4.23.
Zero-based vs one-based APIs
4.24.
SQL building in Kotlin
4.24.1.
Kotlin BOOLEAN value expressions
4.24.2.
Kotlin ARRAY access (new)
4.25.
SQL building in Scala

previous : next

Feedback

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

The jOOQ Logo