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 Settings
4.2.6.1.
Auto-attach Records
4.2.6.2.
Backslash Escaping
4.2.6.3.
Execute Logging
4.2.6.4.
Fetch Warnings
4.2.6.5.
Identifier style
4.2.6.6.
IN-list Padding
4.2.6.7.
JDBC Flags
4.2.6.8.
Keyword style
4.2.6.9.
Map JPA Annotations
4.2.6.10.
Object qualification
4.2.6.11.
Optimistic Locking
4.2.6.12.
Parameter types
4.2.6.13.
Reflection caching
4.2.6.14.
Return all columns on store
4.2.6.15.
Runtime schema and table mapping
4.2.6.16.
Scalar subqueries for stored functions
4.2.6.17.
Statement Type
4.2.6.18.
Updatable Primary Keys
4.2.7.
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 DISTINCT
4.3.4.1.4.
Convenience methods
4.3.4.2.
FROM clause
4.3.4.3.
JOIN operator
4.3.4.4.
WHERE clause
4.3.4.5.
CONNECT BY clause
4.3.4.6.
GROUP BY clause
4.3.4.6.1.
GROUP BY columns
4.3.4.6.2.
GROUP BY ROLLUP
4.3.4.6.3.
GROUP BY CUBE
4.3.4.6.4.
GROUP BY GROUPING SETS
4.3.4.6.5.
GROUP BY empty grouping set
4.3.4.7.
HAVING clause
4.3.4.8.
WINDOW clauseWINDOW clause
4.3.4.9.
ORDER BY clause
4.3.4.9.1.
Ordering by field index
4.3.4.9.2.
Ordering and NULLS
4.3.4.9.3.
Ordering using CASE expressions
4.3.4.9.4.
Oracle's ORDER SIBLINGS BY clause
4.3.4.10.
LIMIT .. OFFSET clause
4.3.4.11.
WITH TIES clause
4.3.4.12.
SEEK clause
4.3.4.13.
FOR UPDATE clause
4.3.4.14.
Set operations
4.3.4.14.1.
Type safety
4.3.4.14.2.
Projection rowtype
4.3.4.14.3.
Differences to standard SQL
4.3.4.14.4.
UNION
4.3.4.14.5.
INTERSECT
4.3.4.14.6.
EXCEPT
4.3.4.15.
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 ROW
4.3.6.3.
UPDATE .. FROM
4.3.6.4.
UPDATE .. WHERE
4.3.6.5.
UPDATE .. RETURNING
4.3.7.
The DELETE statement
4.3.7.1.
DELETE .. WHERE
4.3.7.2.
UPDATE .. RETURNING
4.3.8.
The MERGE statement
4.4.
SQL Statements (DDL)
4.4.1.
The ALTER statement
4.4.1.1.
ALTER INDEX
4.4.1.1.1.
ALTER INDEX .. RENAME
4.4.1.1.2.
ALTER INDEX IF EXISTS
4.4.1.2.
ALTER SCHEMA
4.4.1.2.1.
ALTER SCHEMA .. RENAME
4.4.1.2.2.
ALTER SCHEMA IF EXISTS
4.4.1.3.
ALTER SEQUENCE
4.4.1.3.1.
ALTER SEQUENCE .. RENAME
4.4.1.3.2.
ALTER SEQUENCE .. RESTART
4.4.1.3.3.
ALTER SEQUENCE IF EXISTS
4.4.1.4.
ALTER TABLE
4.4.1.4.1.
ALTER TABLE .. ADD COLUMN
4.4.1.4.2.
ALTER TABLE .. ADD COLUMN IF NOT EXISTS
4.4.1.4.3.
ALTER TABLE .. ADD PRIMARY KEY
4.4.1.4.4.
ALTER TABLE .. ADD UNIQUE
4.4.1.4.5.
ALTER TABLE .. ADD FOREIGN KEY
4.4.1.4.6.
ALTER TABLE .. ADD CHECK
4.4.1.4.7.
ALTER TABLE .. RENAME
4.4.1.4.8.
ALTER TABLE .. ALTER COLUMN .. SET DEFAULT
4.4.1.4.9.
ALTER TABLE .. ALTER COLUMN .. SET NOT NULL
4.4.1.4.10.
ALTER TABLE .. ALTER COLUMN .. DROP NOT NULL
4.4.1.4.11.
ALTER TABLE .. ALTER COLUMN .. SET TYPE
4.4.1.4.12.
ALTER TABLE .. RENAME COLUMN
4.4.1.4.13.
ALTER TABLE .. RENAME CONSTRAINT
4.4.1.4.14.
ALTER TABLE .. RENAME INDEX
4.4.1.4.15.
ALTER TABLE .. DROP COLUMN
4.4.1.4.16.
ALTER TABLE .. DROP COLUMN RESTRICT
4.4.1.4.17.
ALTER TABLE .. DROP COLUMN CASCADE
4.4.1.4.18.
ALTER TABLE .. ADD COLUMN IF NOT EXISTS
4.4.1.4.19.
ALTER TABLE .. DROP CONSTRAINT
4.4.1.4.20.
ALTER TABLE IF EXISTS
4.4.1.5.
ALTER VIEW
4.4.1.5.1.
ALTER VIEW .. RENAME
4.4.1.5.2.
ALTER VIEW IF EXISTS
4.4.2.
The CREATE statement
4.4.2.1.
CREATE INDEX
4.4.2.2.
CREATE SCHEMA
4.4.2.3.
CREATE SEQUENCE
4.4.2.3.1.
CREATE SEQUENCE IF NOT EXISTS
4.4.2.4.
CREATE TABLE
4.4.2.4.1.
Columns
4.4.2.4.2.
Nullability
4.4.2.4.3.
Defaults
4.4.2.4.4.
Identities
4.4.2.4.5.
Primary key
4.4.2.4.6.
Unique constraints
4.4.2.4.7.
Foreign keys
4.4.2.4.8.
Check constraints
4.4.2.4.9.
From a SELECT
4.4.2.4.10.
Temporary tables
4.4.2.5.
CREATE VIEW
4.4.2.5.1.
WITH CHECK OPTION
4.4.2.5.2.
WITH READ ONLY
4.4.3.
The DROP statement
4.4.3.1.
DROP INDEX
4.4.3.1.1.
DROP INDEX IF EXISTS
4.4.3.2.
DROP SCHEMA
4.4.3.2.1.
DROP SCHEMA IF EXISTS
4.4.3.3.
DROP SEQUENCE
4.4.3.3.1.
DROP SEQUENCE IF EXISTS
4.4.3.4.
DROP TABLE
4.4.3.4.1.
DROP TABLE IF EXISTS
4.4.3.5.
DROP VIEW
4.4.3.5.1.
DROP VIEW IF EXISTS
4.4.4.
The SET statement
4.4.4.1.
SET CATALOG
4.4.4.2.
SET SCHEMA
4.4.5.
The TRUNCATE statement
4.4.6.
Generating DDL from objects
4.5.
Catalog and schema expressions
4.6.
Table expressions
4.6.1.
Generated Tables
4.6.2.
Aliased Tables
4.6.2.1.
Aliased generated tables
4.6.2.2.
Aliased table expressions
4.6.2.3.
Derived column lists
4.6.2.4.
Unnamed derived tables
4.6.3.
Joined tables
4.6.3.1.
CROSS JOIN
4.6.3.2.
INNER JOIN
4.6.3.3.
OUTER JOIN
4.6.3.4.
SEMI JOIN
4.6.3.5.
ANTI JOIN
4.6.3.6.
ON clause
4.6.3.7.
ON KEY clause
4.6.3.8.
USING clause
4.6.3.9.
NATURAL clause
4.6.3.10.
LATERAL
4.6.3.11.
APPLY
4.6.3.12.
PARTITION BY
4.6.4.
The VALUES() table constructor
4.6.5.
Derived tables
4.6.6.
The Oracle PIVOT clause
4.6.7.
Relational division
4.6.8.
Array and cursor unnesting
4.6.9.
Table-valued functions
4.6.10.
GENERATE_SERIES
4.6.11.
The DUAL table
4.7.
Column expressions
4.7.1.
Table columns
4.7.1.1.
Generated table columns
4.7.1.2.
Dereferenced table columns
4.7.1.3.
Named table columns
4.7.2.
Aliased columns
4.7.3.
Cast expressions
4.7.4.
Datatype coercions
4.7.5.
Arithmetic expressions
4.7.6.
String concatenation
4.7.7.
Case sensitivity with strings
4.7.8.
General functions
4.7.8.1.
COALESCE
4.7.8.2.
DECODE
4.7.8.3.
NULLIF
4.7.8.4.
NVL
4.7.8.5.
NVL2
4.7.9.
Numeric functions
4.7.9.1.
ABS
4.7.9.2.
ACOS
4.7.9.3.
ASIN
4.7.9.4.
ATAN
4.7.9.5.
ATAN2
4.7.9.6.
CEIL
4.7.9.7.
COS
4.7.9.8.
COSH
4.7.9.9.
COT
4.7.9.10.
COTH
4.7.9.11.
DEG
4.7.9.12.
E
4.7.9.13.
EXP
4.7.9.14.
FLOOR
4.7.9.15.
GREATEST
4.7.9.16.
LEAST
4.7.9.17.
LN
4.7.9.18.
LOG
4.7.9.19.
NEG
4.7.9.20.
PI
4.7.9.21.
POWER
4.7.9.22.
RAD
4.7.9.23.
RAND
4.7.9.24.
ROUND
4.7.9.25.
SIGN
4.7.9.26.
SIN
4.7.9.27.
SINH
4.7.9.28.
SQRT
4.7.9.29.
TAN
4.7.9.30.
TANH
4.7.9.31.
TRUNC
4.7.9.32.
WIDTH_BUCKET
4.7.10.
Bitwise functions
4.7.10.1.
BIT_AND
4.7.10.2.
BIT_COUNT
4.7.10.3.
BIT_NAND
4.7.10.4.
BIT_NOR
4.7.10.5.
BIT_NOT
4.7.10.6.
BIT_OR
4.7.10.7.
BIT_XNOR
4.7.10.8.
BIT_XOR
4.7.10.9.
SHL
4.7.10.10.
SHR
4.7.11.
String functions
4.7.11.1.
ASCII
4.7.11.2.
BIT_LENGTH
4.7.11.3.
CONCAT (|| operator)
4.7.11.4.
LEFT
4.7.11.5.
LENGTH
4.7.11.6.
LOWER
4.7.11.7.
LPAD
4.7.11.8.
LTRIM
4.7.11.9.
MD5
4.7.11.10.
MID
4.7.11.11.
OCTET_LENGTH
4.7.11.12.
POSITION
4.7.11.13.
REPEAT
4.7.11.14.
REPLACE
4.7.11.15.
REVERSE
4.7.11.16.
RIGHT
4.7.11.17.
RPAD
4.7.11.18.
RTRIM
4.7.11.19.
SPACE
4.7.11.20.
SUBSTRING
4.7.11.21.
TRANSLATE
4.7.11.22.
TRIM
4.7.11.23.
UPPER
4.7.12.
Datetime functions
4.7.12.1.
CURRENT_DATE
4.7.12.2.
CURRENT_LOCALDATE
4.7.12.3.
CURRENT_LOCALDATETIME
4.7.12.4.
CURRENT_LOCALTIME
4.7.12.5.
CURRENT_OFFSETDATETIME
4.7.12.6.
CURRENT_OFFSETTIME
4.7.12.7.
CURRENT_TIME
4.7.12.8.
CURRENT_TIMESTAMP
4.7.12.9.
DATE
4.7.12.10.
DATEADD
4.7.12.11.
DATEDIFF
4.7.12.12.
DATESUB
4.7.12.13.
DAY
4.7.12.14.
EXTRACT
4.7.12.15.
HOUR
4.7.12.16.
LOCALDATE
4.7.12.17.
LOCALDATEADD
4.7.12.18.
LOCALDATESUB
4.7.12.19.
LOCALDATETIME
4.7.12.20.
LOCALTIME
4.7.12.21.
MINUTE
4.7.12.22.
MONTH
4.7.12.23.
SECOND
4.7.12.24.
TIME
4.7.12.25.
TIMESTAMP
4.7.12.26.
TIMESTAMPADD
4.7.12.27.
TO_DATE
4.7.12.28.
TO_LOCALDATE
4.7.12.29.
TO_LOCALDATETIME
4.7.12.30.
TO_TIMESTAMP
4.7.12.31.
TRUNC
4.7.12.32.
YEAR
4.7.13.
ARRAY functions
4.7.13.1.
ARRAY constructor
4.7.14.
System functions
4.7.14.1.
CURRENT_SCHEMA
4.7.14.2.
CURRENT_USER
4.7.15.
Aggregate functions
4.7.15.1.
Grouping
4.7.15.2.
Distinctness
4.7.15.3.
Filtering
4.7.15.4.
Ordering
4.7.15.5.
Ordering WITHIN GROUP
4.7.15.6.
Keeping
4.7.15.7.
ARRAY_AGG
4.7.15.8.
AVG
4.7.15.9.
BOOL_AND
4.7.15.10.
BOOL_OR
4.7.15.11.
COLLECT
4.7.15.12.
COUNT
4.7.15.13.
CUME_DIST
4.7.15.14.
DENSE_RANK
4.7.15.15.
EVERY
4.7.15.16.
GROUP_CONCAT
4.7.15.17.
LISTAGG
4.7.15.18.
MAX
4.7.15.19.
MEDIAN
4.7.15.20.
MIN
4.7.15.21.
MODE
4.7.15.22.
PERCENT_RANK
4.7.15.23.
PERCENTILE_CONT
4.7.15.24.
PERCENTILE_DISC
4.7.15.25.
PRODUCT
4.7.15.26.
RANK
4.7.15.27.
SUM
4.7.16.
Window functions
4.7.16.1.
PARTITION BY
4.7.16.2.
ORDER BY
4.7.16.3.
ROWS, RANGE, GROUPS (frame clause)
4.7.16.4.
EXCLUDE
4.7.16.5.
NULL treatment
4.7.16.6.
FROM FIRST, FROM LAST
4.7.16.7.
Nested aggregate functions
4.7.16.8.
Window aggregation
4.7.16.9.
Window ordered aggregate
4.7.16.10.
ROW_NUMBER
4.7.16.11.
RANK
4.7.16.12.
DENSE_RANK
4.7.16.13.
PERCENT_RANK
4.7.16.14.
CUME_DIST
4.7.16.15.
NTILE
4.7.16.16.
LEAD
4.7.16.17.
LAG
4.7.16.18.
FIRST_VALUE
4.7.16.19.
LAST_VALUE
4.7.16.20.
NTH_VALUE
4.7.17.
User-defined functions
4.7.18.
User-defined aggregate functions
4.7.19.
The CASE expression
4.7.20.
Sequences and serials
4.7.21.
Scalar subqueries
4.7.22.
Tuples or row value expressions
4.8.
Conditional expressions
4.8.1.
Condition building
4.8.2.
TRUE and FALSE condition
4.8.3.
BOOLEAN columns
4.8.4.
AND, OR, NOT boolean operators
4.8.5.
Boolean operator precedence
4.8.6.
Comparison predicate
4.8.7.
Comparison predicate (degree > 1)
4.8.8.
Quantified comparison predicate
4.8.9.
BETWEEN predicate
4.8.10.
BETWEEN predicate (degree > 1)
4.8.11.
DISTINCT predicate
4.8.12.
LIKE predicate
4.8.13.
NULL predicate
4.8.14.
NULL predicate (degree > 1)
4.8.15.
EXISTS predicate
4.8.16.
IN predicate
4.8.17.
IN predicate (degree > 1)
4.8.18.
OVERLAPS predicate
4.8.19.
Query By Example (QBE)
4.9.
Operator precedence
4.10.
Synthetic SQL clauses
4.11.
Dynamic SQL
4.12.
Plain SQL
4.13.
Plain SQL Templating Language
4.14.
Hints
4.14.1.
MySQL hints
4.14.1.1.
Index hints
4.14.1.2.
STRAIGHT_JOIN
4.14.1.3.
Oracle style hints in MySQL
4.14.2.
Oracle hints
4.14.3.
SQL Server hints
4.14.3.1.
WITH
4.14.3.2.
OPTION
4.15.
SQL Parser
4.15.1.
SQL Parser API
4.15.2.
SQL translator
4.15.3.
SQL Parser Grammar
4.16.
Names and identifiers
4.17.
Bind values and parameters
4.17.1.
Indexed parameters
4.17.2.
Named parameters
4.17.3.
Inlined parameters
4.17.4.
SQL injection
4.18.
QueryParts
4.18.1.
SQL rendering
4.18.2.
Declaration vs reference
4.18.3.
Pretty printing SQL
4.18.4.
Variable binding
4.18.5.
Custom data type bindings
4.18.6.
Custom syntax elements
4.18.7.
Plain SQL QueryParts
4.18.8.
Serializability
4.18.9.
Custom SQL transformation with VisitListener
4.18.9.1.
Example: Logging abbreviated bind values
4.19.
Zero-based vs one-based APIs
4.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