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

3.1.
The query DSL type
3.1.1.
DSL subclasses
3.2.
The DSLContext API
3.2.1.
SQL Dialect
3.2.2.
SQL Dialect Family
3.2.3.
Connection vs. DataSource
3.2.4.
Custom data
3.2.5.
Custom ExecuteListeners
3.2.6.
Custom Settings
3.2.6.1.
Auto-attach Records
3.2.6.2.
Backslash Escaping
3.2.6.3.
Execute Logging
3.2.6.4.
Fetch Warnings
3.2.6.5.
Identifier style
3.2.6.6.
IN-list Padding
3.2.6.7.
JDBC Flags
3.2.6.8.
Keyword style
3.2.6.9.
Map JPA Annotations
3.2.6.10.
Object qualification
3.2.6.11.
Optimistic Locking
3.2.6.12.
Parameter types
3.2.6.13.
Reflection caching
3.2.6.14.
Return all columns on store
3.2.6.15.
Runtime schema and table mapping
3.2.6.16.
Scalar subqueries for stored functions
3.2.6.17.
Statement Type
3.2.6.18.
Updatable Primary Keys
3.2.7.
Thread safety
3.3.
SQL Statements (DML)
3.3.1.
jOOQ's DSL and model API
3.3.2.
The WITH clause
3.3.3.
The WITH RECURSIVE clause
3.3.4.
The SELECT statement
3.3.4.1.
SELECT clause
3.3.4.1.1.
Projection type safety
3.3.4.1.2.
SelectField
3.3.4.1.3.
SELECT * (new)
3.3.4.1.4.
SELECT DISTINCT
3.3.4.1.5.
SELECT DISTINCT ON (new)
3.3.4.1.6.
Convenience methods
3.3.4.2.
FROM clause
3.3.4.3.
JOIN operator
3.3.4.4.
Implicit path JOIN (new)
3.3.4.5.
WHERE clause
3.3.4.6.
CONNECT BY clause
3.3.4.7.
GROUP BY clause
3.3.4.7.1.
GROUP BY columns
3.3.4.7.2.
GROUP BY ROLLUP
3.3.4.7.3.
GROUP BY CUBE
3.3.4.7.4.
GROUP BY GROUPING SETS
3.3.4.7.5.
GROUP BY empty grouping set
3.3.4.8.
HAVING clause
3.3.4.9.
WINDOW clauseWINDOW clause
3.3.4.10.
ORDER BY clause
3.3.4.10.1.
Ordering by field index
3.3.4.10.2.
Ordering and NULLS
3.3.4.10.3.
Ordering using CASE expressions
3.3.4.10.4.
Oracle's ORDER SIBLINGS BY clause
3.3.4.11.
LIMIT .. OFFSET clause
3.3.4.12.
WITH TIES clause
3.3.4.13.
SEEK clause
3.3.4.14.
FOR UPDATE clause
3.3.4.15.
Set operations
3.3.4.15.1.
Type safety
3.3.4.15.2.
Projection rowtype
3.3.4.15.3.
Differences to standard SQL
3.3.4.15.4.
UNION
3.3.4.15.5.
INTERSECT
3.3.4.15.6.
EXCEPT
3.3.4.16.
Lexical and logical SELECT clause order
3.3.5.
The INSERT statement
3.3.5.1.
INSERT .. VALUES
3.3.5.2.
INSERT .. DEFAULT VALUES
3.3.5.3.
INSERT .. SET
3.3.5.4.
INSERT .. SELECT
3.3.5.5.
INSERT .. ON DUPLICATE KEY UPDATE
3.3.5.6.
INSERT .. ON DUPLICATE KEY IGNORE
3.3.5.7.
INSERT .. ON CONFLICT
3.3.5.8.
INSERT .. RETURNING
3.3.6.
The UPDATE statement
3.3.6.1.
UPDATE .. SET
3.3.6.2.
UPDATE .. SET ROW
3.3.6.3.
UPDATE .. FROM
3.3.6.4.
UPDATE .. WHERE
3.3.6.5.
UPDATE .. RETURNING
3.3.7.
The DELETE statement
3.3.7.1.
DELETE .. WHERE
3.3.7.2.
UPDATE .. RETURNING
3.3.8.
The MERGE statement
3.4.
SQL Statements (DDL)
3.4.1.
The ALTER statement
3.4.1.1.
ALTER INDEX
3.4.1.1.1.
ALTER INDEX .. RENAME
3.4.1.1.2.
ALTER INDEX IF EXISTS
3.4.1.2.
ALTER SCHEMA
3.4.1.2.1.
ALTER SCHEMA .. RENAME
3.4.1.2.2.
ALTER SCHEMA IF EXISTS
3.4.1.3.
ALTER SEQUENCE
3.4.1.3.1.
ALTER SEQUENCE .. RENAME
3.4.1.3.2.
ALTER SEQUENCE .. RESTART
3.4.1.3.3.
ALTER SEQUENCE IF EXISTS
3.4.1.4.
ALTER TABLE
3.4.1.4.1.
ALTER TABLE .. ADD COLUMN
3.4.1.4.2.
ALTER TABLE .. ADD COLUMNS (new)
3.4.1.4.3.
ALTER TABLE .. ADD COLUMN IF NOT EXISTS
3.4.1.4.4.
ALTER TABLE .. ADD PRIMARY KEY
3.4.1.4.5.
ALTER TABLE .. ADD UNIQUE
3.4.1.4.6.
ALTER TABLE .. ADD FOREIGN KEY
3.4.1.4.7.
ALTER TABLE .. ADD CHECK
3.4.1.4.8.
ALTER TABLE .. RENAME
3.4.1.4.9.
ALTER TABLE .. COMMENT (new)
3.4.1.4.10.
ALTER TABLE .. ALTER COLUMN .. SET DEFAULT
3.4.1.4.11.
ALTER TABLE .. ALTER COLUMN .. SET NOT NULL
3.4.1.4.12.
ALTER TABLE .. ALTER COLUMN .. DROP NOT NULL
3.4.1.4.13.
ALTER TABLE .. ALTER COLUMN .. SET TYPE
3.4.1.4.14.
ALTER TABLE .. RENAME COLUMN
3.4.1.4.15.
ALTER TABLE .. RENAME CONSTRAINT
3.4.1.4.16.
ALTER TABLE .. RENAME INDEX
3.4.1.4.17.
ALTER TABLE .. DROP COLUMN
3.4.1.4.18.
ALTER TABLE .. DROP COLUMN RESTRICT
3.4.1.4.19.
ALTER TABLE .. DROP COLUMN CASCADE
3.4.1.4.20.
ALTER TABLE .. DROP COLUMNS (new)
3.4.1.4.21.
ALTER TABLE .. ADD COLUMN IF NOT EXISTS
3.4.1.4.22.
ALTER TABLE .. DROP CONSTRAINT
3.4.1.4.23.
ALTER TABLE IF EXISTS
3.4.1.5.
ALTER VIEW
3.4.1.5.1.
ALTER VIEW .. COMMENT (new)
3.4.1.5.2.
ALTER VIEW .. RENAME
3.4.1.5.3.
ALTER VIEW IF EXISTS
3.4.2.
The COMMENT statement (new)
3.4.2.1.
COMMENT ON TABLE (new)
3.4.2.2.
COMMENT ON VIEW (new)
3.4.2.3.
COMMENT ON COLUMN (new)
3.4.3.
The CREATE statement
3.4.3.1.
CREATE INDEX
3.4.3.2.
CREATE SCHEMA
3.4.3.3.
CREATE SEQUENCE
3.4.3.3.1.
CREATE SEQUENCE IF NOT EXISTS
3.4.3.4.
CREATE TABLE
3.4.3.4.1.
Columns
3.4.3.4.2.
Nullability
3.4.3.4.3.
Defaults
3.4.3.4.4.
Identities
3.4.3.4.5.
Primary key
3.4.3.4.6.
Unique constraints
3.4.3.4.7.
Foreign keys
3.4.3.4.8.
Check constraints
3.4.3.4.9.
From a SELECT
3.4.3.4.10.
Temporary tables
3.4.3.5.
CREATE VIEW
3.4.3.5.1.
CREATE OR REPLACE VIEW (new)
3.4.3.5.2.
WITH CHECK OPTION
3.4.3.5.3.
WITH READ ONLY
3.4.4.
The DROP statement
3.4.4.1.
DROP INDEX
3.4.4.1.1.
DROP INDEX IF EXISTS
3.4.4.2.
DROP SCHEMA
3.4.4.2.1.
DROP SCHEMA IF EXISTS
3.4.4.3.
DROP SEQUENCE
3.4.4.3.1.
DROP SEQUENCE IF EXISTS
3.4.4.4.
DROP TABLE
3.4.4.4.1.
DROP TABLE IF EXISTS
3.4.4.5.
DROP VIEW
3.4.4.5.1.
DROP VIEW IF EXISTS
3.4.5.
The GRANT statement (new)
3.4.6.
The REVOKE statement (new)
3.4.7.
The SET statement
3.4.7.1.
SET CATALOG
3.4.7.2.
SET SCHEMA
3.4.8.
The TRUNCATE statement
3.4.9.
Generating DDL from objects
3.5.
Catalog and schema expressions
3.6.
Table expressions
3.6.1.
Generated Tables
3.6.2.
Aliased Tables
3.6.2.1.
Aliased generated tables
3.6.2.2.
Aliased table expressions
3.6.2.3.
Derived column lists
3.6.2.4.
Unnamed derived tables
3.6.3.
Joined tables
3.6.3.1.
CROSS JOIN
3.6.3.2.
INNER JOIN
3.6.3.3.
OUTER JOIN
3.6.3.4.
SEMI JOIN
3.6.3.5.
ANTI JOIN
3.6.3.6.
ON clause
3.6.3.7.
ON KEY clause
3.6.3.8.
USING clause
3.6.3.9.
NATURAL clause
3.6.3.10.
LATERAL
3.6.3.11.
APPLY
3.6.3.12.
PARTITION BY
3.6.4.
The VALUES() table constructor
3.6.5.
Derived tables
3.6.6.
The Oracle PIVOT clause
3.6.7.
Relational division
3.6.8.
Array and cursor unnesting
3.6.9.
Table-valued functions
3.6.10.
GENERATE_SERIES
3.6.11.
The DUAL table
3.7.
Column expressions
3.7.1.
Table columns
3.7.1.1.
Generated table columns
3.7.1.2.
Dereferenced table columns
3.7.1.3.
Named table columns
3.7.2.
Aliased columns
3.7.3.
Cast expressions
3.7.4.
Datatype coercions
3.7.5.
Collations (new)
3.7.6.
Arithmetic expressions
3.7.7.
String concatenation
3.7.8.
Case sensitivity with strings
3.7.9.
General functions
3.7.9.1.
CHOOSE (new)
3.7.9.2.
COALESCE
3.7.9.3.
DECODE
3.7.9.4.
IIF (new)
3.7.9.5.
NULLIF
3.7.9.6.
NVL
3.7.9.7.
NVL2
3.7.10.
Numeric functions
3.7.10.1.
ABS
3.7.10.2.
ACOS
3.7.10.3.
ASIN
3.7.10.4.
ATAN
3.7.10.5.
ATAN2
3.7.10.6.
CEIL
3.7.10.7.
COS
3.7.10.8.
COSH
3.7.10.9.
COT
3.7.10.10.
COTH
3.7.10.11.
DEG
3.7.10.12.
E
3.7.10.13.
EXP
3.7.10.14.
FLOOR
3.7.10.15.
GREATEST
3.7.10.16.
LEAST
3.7.10.17.
LN
3.7.10.18.
LOG
3.7.10.19.
NEG
3.7.10.20.
PI
3.7.10.21.
POWER
3.7.10.22.
RAD
3.7.10.23.
RAND
3.7.10.24.
ROUND
3.7.10.25.
SIGN
3.7.10.26.
SIN
3.7.10.27.
SINH
3.7.10.28.
SQRT
3.7.10.29.
TAN
3.7.10.30.
TANH
3.7.10.31.
TRUNC
3.7.10.32.
WIDTH_BUCKET
3.7.11.
Bitwise functions
3.7.11.1.
BIT_AND
3.7.11.2.
BIT_COUNT
3.7.11.3.
BIT_NAND
3.7.11.4.
BIT_NOR
3.7.11.5.
BIT_NOT
3.7.11.6.
BIT_OR
3.7.11.7.
BIT_XNOR
3.7.11.8.
BIT_XOR
3.7.11.9.
SHL
3.7.11.10.
SHR
3.7.12.
String functions
3.7.12.1.
ASCII
3.7.12.2.
BIT_LENGTH
3.7.12.3.
CONCAT (|| operator)
3.7.12.4.
LEFT
3.7.12.5.
LENGTH
3.7.12.6.
LOWER
3.7.12.7.
LPAD
3.7.12.8.
LTRIM
3.7.12.9.
MD5
3.7.12.10.
MID
3.7.12.11.
OCTET_LENGTH
3.7.12.12.
POSITION
3.7.12.13.
REPEAT
3.7.12.14.
REPLACE
3.7.12.15.
REVERSE
3.7.12.16.
RIGHT
3.7.12.17.
RPAD
3.7.12.18.
RTRIM
3.7.12.19.
SPACE
3.7.12.20.
SUBSTRING
3.7.12.21.
TRANSLATE
3.7.12.22.
TRIM
3.7.12.23.
UPPER
3.7.13.
Datetime functions
3.7.13.1.
CURRENT_DATE
3.7.13.2.
CURRENT_LOCALDATE
3.7.13.3.
CURRENT_LOCALDATETIME
3.7.13.4.
CURRENT_LOCALTIME
3.7.13.5.
CURRENT_OFFSETDATETIME
3.7.13.6.
CURRENT_OFFSETTIME
3.7.13.7.
CURRENT_TIME
3.7.13.8.
CURRENT_TIMESTAMP
3.7.13.9.
DATE
3.7.13.10.
DATEADD
3.7.13.11.
DATEDIFF
3.7.13.12.
DATESUB
3.7.13.13.
DAY
3.7.13.14.
EXTRACT
3.7.13.15.
HOUR
3.7.13.16.
LOCALDATE
3.7.13.17.
LOCALDATEADD
3.7.13.18.
LOCALDATESUB
3.7.13.19.
LOCALDATETIME
3.7.13.20.
LOCALDATETIMEADD (new)
3.7.13.21.
LOCALDATETIMESUB (new)
3.7.13.22.
LOCALTIME
3.7.13.23.
MINUTE
3.7.13.24.
MONTH
3.7.13.25.
SECOND
3.7.13.26.
TIME
3.7.13.27.
TIMESTAMP
3.7.13.28.
TIMESTAMPADD
3.7.13.29.
TIMESTAMPSUB (new)
3.7.13.30.
TO_DATE
3.7.13.31.
TO_LOCALDATE
3.7.13.32.
TO_LOCALDATETIME
3.7.13.33.
TO_TIMESTAMP
3.7.13.34.
TRUNC
3.7.13.35.
YEAR
3.7.14.
ARRAY functions
3.7.14.1.
ARRAY constructor
3.7.15.
System functions
3.7.15.1.
CURRENT_SCHEMA
3.7.15.2.
CURRENT_USER
3.7.16.
Aggregate functions
3.7.16.1.
Grouping
3.7.16.2.
Distinctness
3.7.16.3.
Filtering
3.7.16.4.
Ordering
3.7.16.5.
Ordering WITHIN GROUP
3.7.16.6.
Keeping
3.7.16.7.
ARRAY_AGG
3.7.16.8.
AVG
3.7.16.9.
BOOL_AND
3.7.16.10.
BOOL_OR
3.7.16.11.
COLLECT
3.7.16.12.
COUNT
3.7.16.13.
CUME_DIST
3.7.16.14.
DENSE_RANK
3.7.16.15.
EVERY
3.7.16.16.
GROUP_CONCAT
3.7.16.17.
LISTAGG
3.7.16.18.
MAX
3.7.16.19.
MEDIAN
3.7.16.20.
MIN
3.7.16.21.
MODE
3.7.16.22.
PERCENT_RANK
3.7.16.23.
PERCENTILE_CONT
3.7.16.24.
PERCENTILE_DISC
3.7.16.25.
PRODUCT
3.7.16.26.
RANK
3.7.16.27.
SUM
3.7.17.
Window functions
3.7.17.1.
PARTITION BY
3.7.17.2.
ORDER BY
3.7.17.3.
ROWS, RANGE, GROUPS (frame clause)
3.7.17.4.
EXCLUDE
3.7.17.5.
NULL treatment
3.7.17.6.
FROM FIRST, FROM LAST
3.7.17.7.
Nested aggregate functions
3.7.17.8.
Window aggregation
3.7.17.9.
Window ordered aggregate
3.7.17.10.
ROW_NUMBER
3.7.17.11.
RANK
3.7.17.12.
DENSE_RANK
3.7.17.13.
PERCENT_RANK
3.7.17.14.
CUME_DIST
3.7.17.15.
NTILE
3.7.17.16.
LEAD
3.7.17.17.
LAG
3.7.17.18.
FIRST_VALUE
3.7.17.19.
LAST_VALUE
3.7.17.20.
NTH_VALUE
3.7.18.
User-defined functions
3.7.19.
User-defined aggregate functions
3.7.20.
The CASE expression
3.7.21.
Sequences and serials
3.7.22.
Scalar subqueries
3.7.23.
Tuples or row value expressions
3.8.
Conditional expressions
3.8.1.
Condition building
3.8.2.
TRUE and FALSE condition
3.8.3.
BOOLEAN columns
3.8.4.
AND, OR, NOT boolean operators
3.8.5.
Boolean operator precedence
3.8.6.
Comparison predicate
3.8.7.
Comparison predicate (degree > 1)
3.8.8.
Quantified comparison predicate
3.8.9.
BETWEEN predicate
3.8.10.
BETWEEN predicate (degree > 1)
3.8.11.
DISTINCT predicate
3.8.12.
DISTINCT predicate (degree > 1) (new)
3.8.13.
LIKE predicate
3.8.14.
NULL predicate
3.8.15.
NULL predicate (degree > 1)
3.8.16.
EXISTS predicate
3.8.17.
IN predicate
3.8.18.
IN predicate (degree > 1)
3.8.19.
OVERLAPS predicate
3.8.20.
Query By Example (QBE)
3.9.
Operator precedence
3.10.
Synthetic SQL clauses
3.11.
Dynamic SQL
3.11.1.
Optional conditional expressions (new)
3.12.
Plain SQL
3.13.
Plain SQL Templating Language
3.14.
Hints
3.14.1.
MySQL hints
3.14.1.1.
Index hints
3.14.1.2.
STRAIGHT_JOIN
3.14.1.3.
Oracle style hints in MySQL
3.14.2.
Oracle hints
3.14.3.
SQL Server hints
3.14.3.1.
WITH
3.14.3.2.
OPTION
3.15.
SQL Parser
3.15.1.
SQL Parser API
3.15.2.
SQL Parser CLI (new)
3.15.3.
SQL translator
3.15.4.
SQL Parser Grammar
3.16.
Names and identifiers
3.17.
Bind values and parameters
3.17.1.
Indexed parameters
3.17.2.
Named parameters
3.17.3.
Inlined parameters
3.17.4.
SQL injection
3.18.
QueryParts
3.18.1.
SQL rendering
3.18.2.
Declaration vs reference
3.18.3.
Pretty printing SQL
3.18.4.
Variable binding
3.18.5.
Custom data type bindings
3.18.6.
Custom syntax elements
3.18.7.
Plain SQL QueryParts
3.18.8.
Serializability
3.18.9.
Custom SQL transformation with VisitListener
3.18.9.1.
Example: Logging abbreviated bind values
3.19.
Zero-based vs one-based APIs
3.20.
SQL building in Scala

previous : next

Feedback

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

The jOOQ Logo