Skip to main content

Select fields

By default, when a query returns records (as opposed to a count), the result includes the default selection set:

  • All scalar fields defined in the Prisma schema (including enums)
  • None of the relations

To customize the result:

Selecting only the fields and relations that you require rather than relying on the default selection set can ✔ reduce the size of the response and ✔ improve query speed.

Since version 5.9.0, when doing a relation query with include or by using select on a relation field, you can also specify the relationLoadStrategy to decide whether you want to use a database-level JOIN or perform multiple queries and merge the data on the application level. This feature is currently in Preview, you can learn more about it here.

Example schema

All examples are based on the following schema:

Expand for sample schema
Relational databases
MongoDB

For relational databases, use db push command to push the example schema to your own database

npx prisma db push

For MongoDB, ensure your data is in a uniform shape and matches the model defined in the Prisma schema.

Return the default selection set

The following query returns the default selection set (all scalar fields, no relations):

Hide query results

Select specific fields

Use select to return a limited subset of fields instead of all fields. The following example returns the email and name fields only:

Hide query results

Include relations and select relation fields

To return specific relation fields, you can:

  • Use a nested select
  • Use a select within an include

To return all relation fields, use include only - for example, { include: { posts: true } }.

The following query uses a nested select to select each user's name and the title of each related post:

Hide query results

The following query uses select within an include, and returns all user fields and each post's title field:

Hide query results

For more information about querying relations, refer to the following documentation:

Relation count

In 3.0.1 and later, you can include or select a count of relations alongside fields - for example, a user's post count.