> For the complete documentation index, see [llms.txt](https://coldbox-orm.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coldbox-orm.ortusbooks.com/v5.x/criteria-queries/detached-criteria-builder.md).

# Subqueries

Subqueries in criteria queries: c.subquery() replaces the cborm 5 Detached Criteria Builder

A subquery is a query inside another query: "users who wrote a post in the last week", "posts more expensive than every book", "roles with no users". In cborm 6 you create one from a criteria with `c.subquery( entityName, alias )` and pass it to a subquery condition of the same criteria (`exists()`, `isIn()`, `propertyIn()`, `subGe()`, ...).

```javascript
// Users who wrote a published post
var c = userService.newCriteria();
var authors = c
    .exists(
        c.subquery( "Post", "p" )
            .eqProperty( "p.author", "this" )
            .isTrue( "isPublished" )
    )
    .list();

// Users with a draft post: their id is in the subquery's projected values
var c = userService.newCriteria();
var drafters = c
    .isIn(
        "id",
        c.subquery( "Post", "p" )
            .isEq( "status", "draft" )
            .withProjections( property = "author.id" )
    )
    .list();
```

A subquery is a criteria builder too: it has the same conditions, joins and projections as the main criteria. It can't be run on its own: calling `list()`, `count()` or `get()` on it is an `orm.argument` error.

* [Getting Started](/v5.x/criteria-queries/detached-criteria-builder/getting-started.md): create a subquery, correlate it with the outer query, add conditions and joins.
* [Projections](/v5.x/criteria-queries/detached-criteria-builder/projections.md): choose what the subquery selects.
* [Subqueries](/v5.x/criteria-queries/detached-criteria-builder/subqueries.md): every condition that takes a subquery, including the quantified `All`/`Some` forms.

{% hint style="info" %}
**Changes from cborm 5**

Hibernate 7 removed `DetachedCriteria`, so cborm's `DetachedCriteriaBuilder` is gone. `c.subquery()` replaces it (`createSubcriteria()` and `detachedCriteria()` are aliases), and the subquery conditions keep their cborm names (`propertyIn`, `subEq`, `subGeAll`, `propertyLtSome`, `exists`, ...). The call order changed: the subquery is now an argument of the condition, added to the outer criteria, instead of a condition called on the subquery and passed to `add()`.

```javascript
// cborm 5
c.add(
    c.createSubcriteria( "Post", "p" )
        .withProjections( property = "author.id" )
        .isEq( "status", "draft" )
        .propertyIn( "id" )
);

// cborm 6
c.propertyIn(
    "id",
    c.subquery( "Post", "p" )
        .withProjections( property = "author.id" )
        .isEq( "status", "draft" )
);
```

Detached criteria can no longer be executed in another session, and `detachedSQLProjection` has no equivalent: see [Projections](/v5.x/criteria-queries/detached-criteria-builder/projections.md).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://coldbox-orm.ortusbooks.com/v5.x/criteria-queries/detached-criteria-builder.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
