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
The SELECT statement
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
When you don't just perform CRUD (i.e. SELECT * FROM your_table WHERE ID = ?), you're usually generating new record types using custom projections. With jOOQ, this is as intuitive, as if using SQL directly. A more or less complete example of the "standard" SQL syntax, plus some extensions, is provided by a query like this:
SELECT from a complex table expression
-- get all authors' first and last names, and the number -- of books they've written in German, if they have written -- more than five books in German in the last three years -- (from 2011), and sort those authors by last names -- limiting results to the second and third row, locking -- the rows for a subsequent update... whew! SELECT AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, COUNT(*) FROM AUTHOR JOIN BOOK ON AUTHOR.ID = BOOK.AUTHOR_ID WHERE BOOK.LANGUAGE = 'DE' AND BOOK.PUBLISHED_IN > 2008 GROUP BY AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME HAVING COUNT(*) > 5 ORDER BY AUTHOR.LAST_NAME ASC NULLS FIRST LIMIT 2 OFFSET 1 FOR UPDATE
// And with jOOQ... DSLContext create = DSL.using(connection, dialect); create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count()) .from(AUTHOR) .join(BOOK).on(BOOK.AUTHOR_ID.eq(AUTHOR.ID)) .where(BOOK.LANGUAGE.eq("DE")) .and(BOOK.PUBLISHED_IN.gt(2008)) .groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) .having(count().gt(5)) .orderBy(AUTHOR.LAST_NAME.asc().nullsFirst()) .limit(2) .offset(1) .forUpdate() .fetch();
Details about the various clauses of this query will be provided in subsequent sections.
SELECT from single tables
A very similar, but limited API is available, if you want to select from single tables in order to retrieve TableRecords or even UpdatableRecords. The decision, which type of select to create is already made at the very first step, when you create the SELECT
statement with the DSL or DSLContext types:
public <R extends Record> SelectWhereStep<R> selectFrom(Table<R> table);
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
BookRecord book = create.selectFrom(BOOK) .where(BOOK.LANGUAGE.eq("DE")) .orderBy(BOOK.TITLE) .fetchAny();
The "reduced" SELECT API is limited in the way that it skips DSL access to any of these clauses:
In most parts of this manual, it is assumed that you do not use the "reduced" SELECT API. For more information about the simple SELECT API, see the manual's section about fetching strongly or weakly typed records.
Table of contents
- 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 *
- 3.3.4.1.4.
- SELECT * EXCEPT (...)
- 3.3.4.1.5.
- SELECT DISTINCT
- 3.3.4.1.6.
- SELECT DISTINCT ON
- 3.3.4.1.7.
- Convenience methods
- 3.3.4.2.
- FROM clause
- 3.3.4.3.
- JOIN operator
- 3.3.4.4.
- Implicit path JOIN
- 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.
- QUALIFY clause
- 3.3.4.11.
- ORDER BY clause
- 3.3.4.11.1.
- Ordering by field index
- 3.3.4.11.2.
- Ordering and NULLS
- 3.3.4.11.3.
- Ordering using CASE expressions
- 3.3.4.11.4.
- Oracle's ORDER SIBLINGS BY clause
- 3.3.4.12.
- LIMIT .. OFFSET clause
- 3.3.4.13.
- WITH TIES clause
- 3.3.4.14.
- SEEK clause
- 3.3.4.15.
- FOR clause (new)
- 3.3.4.16.
- FOR UPDATE clause
- 3.3.4.17.
- Set operations
- 3.3.4.17.1.
- Type safety
- 3.3.4.17.2.
- Projection rowtype
- 3.3.4.17.3.
- Differences to standard SQL
- 3.3.4.17.4.
- UNION
- 3.3.4.17.5.
- INTERSECT
- 3.3.4.17.6.
- EXCEPT
- 3.3.4.18.
- Lexical and logical SELECT clause order
previous : next |
References to this page
- jOOQ and backwards-compatibility
- SQL Dialect
- The WITH clause
- Implicit path JOIN
- FOR clause
- INSERT .. RETURNING
- From a SELECT
- Generated Tables
- Joined tables
- The VALUES() table constructor
- Derived tables
- Table-valued functions
- The DUAL table
- Temporal tables
- Grouping
- Conditional expressions
- Comparison predicate (degree > 1)
- EXISTS predicate
- Synthetic SQL clauses
- SQL Parser API
- Declaration vs reference
- Record vs. TableRecord
- CRUD with UpdatableRecords
- Simple CRUD
- Batched Connection
Feedback
Do you have any feedback about this page? We'd love to hear it!