> 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/projections.md).

# Projections

Choose what a subquery selects with withProjections() or project()

A subquery used in a comparison (`propertyIn()`, `subEq()`, `isIn()`, ...) must select one value per row: a property, an id or an aggregate. You choose it with a projection, exactly as on a main criteria: `withProjections()` or `project()`. See [Projections & Aggregates](/v5.x/criteria-queries/criteria-builder/projections.md).

```javascript
var c = userService.newCriteria();

// The subquery selects the post's author id
var authors = c
    .propertyIn(
        "id",
        c.subquery( "Post", "p" )
            .isEq( "status", "draft" )
            .withProjections( property = "author.id" )
    )
    .list();

// The same with project()
var c = userService.newCriteria();
var authors = c
    .propertyIn(
        "id",
        c.subquery( "Post", "p" )
            .isEq( "status", "draft" )
            .project( ( p ) => p.property( "author.id" ) )
    )
    .list();
```

## What a subquery selects

* The **first** projection is the value the subquery selects. Declare one projection per subquery.
* `groupProperty` projections add a `group by`, for example to compare with a per-group aggregate.
* `distinct` (or `asDistinct()`) makes the subquery `select distinct`.
* Without a projection, the subquery selects its entity, which is all `exists()` and `notExists()` need. For comparisons, always declare a projection.

```javascript
// Aggregates: users whose balance is above the average
var c = userService.newCriteria();
var aboveAverage = c
    .propertyGt( "balance", c.subquery( "User", "u2" ).withProjections( avg = "balance" ) )
    .list();

// Row count: users with more than 10 comments
var c = userService.newCriteria();
var chatty = c
    .subLt( 10, c.subquery( "Comment", "cm" ).eqProperty( "cm.author", "this" ).withProjections( rowCount = true ) )
    .list();

// Id projection: posts whose author is an admin
var c = postService.newCriteria();
var adminPosts = c
    .propertyIn( "author.id", c.subquery( "User", "u" ).isEq( "u.role.slug", "admin" ).withProjections( id = true ) )
    .list();
```

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

* A subquery can't be used as a projection of the outer query: `detachedSQLProjection` has no equivalent. For a computed column such as "number of posts per user", group and count from the other side, or use HQL through `executeQuery()`:

```javascript
// Post count per author, from the Post side
var perAuthor = postService
    .newCriteria()
    .project( ( p ) => p.group( "author.id", "authorId" ).rowCount( "posts" ) )
    .asStruct()
    .list();

// A subquery in the select list, with HQL
var rows = ormService.executeQuery(
    "select u.id as id, ( select count( p ) from Post p where p.author = u ) as posts from User u"
);
```

* You no longer need `this.property` or casts to work around projection aliases in the main criteria: paths are resolved against the entity, and projection aliases only name result columns.
  {% 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/projections.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.
