# Filtering and sorting (/docs/orm/prisma-client/queries/filtering-and-sorting)

> For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL.

Learn how to filter Prisma Client queries with where and sort results with orderBy.

Location: ORM > Prisma Client > Queries > Filtering and sorting

Prisma Client lets you narrow results with `where` and order them with `orderBy`.

## Filtering with where [#filtering-with-where]

Use `where` to match records by field values:

```ts
const users = await prisma.user.findMany({
  where: {
    email: {
      endsWith: "prisma.io",
    },
  },
});
```

## Combining operators [#combining-operators]

You can compose filters with operators such as `OR`, `AND`, and `NOT`:

```ts
const users = await prisma.user.findMany({
  where: {
    OR: [
      { email: { endsWith: "gmail.com" } },
      { email: { endsWith: "company.com" } },
    ],
    NOT: {
      email: {
        endsWith: "admin.company.com",
      },
    },
  },
});
```

## Filter on related records [#filter-on-related-records]

Relation filters let you match records based on related data:

```ts
const users = await prisma.user.findMany({
  where: {
    posts: {
      some: {
        published: true,
      },
    },
  },
});
```

For more relation-specific patterns, see [Relation queries](https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries).

## Sort results with orderBy [#sort-results-with-orderby]

Use `orderBy` to control result ordering:

```ts
const posts = await prisma.post.findMany({
  orderBy: {
    title: "asc",
  },
});
```

You can also combine filtering and sorting:

```ts
const posts = await prisma.post.findMany({
  where: {
    published: true,
  },
  orderBy: {
    createdAt: "desc",
  },
});
```

## Case-insensitive filtering [#case-insensitive-filtering]

Case sensitivity depends on your database provider and collation settings. For PostgreSQL, Prisma Client also supports specific case-insensitive filter modes on supported operators. See the [Prisma Client API reference](https://www.prisma.io/docs/orm/reference/prisma-client-reference#mode) for details.

## Sort by relation [#sort-by-relation]

You can sort by properties on related records when the query shape supports it. For example, you might order posts by their author's name or users by related aggregates.

## Sort by relevance (PostgreSQL and MySQL) [#sort-by-relevance-postgresql-and-mysql]

On supported databases, Prisma Client can sort search results by relevance using `_relevance`. This is especially useful when combined with [full-text search](https://www.prisma.io/docs/orm/prisma-client/queries/full-text-search).

## Sort with null records first or last [#sort-with-null-records-first-or-last]

Prisma Client supports explicit null ordering on supported databases so you can keep incomplete values grouped at the beginning or end of a result set.

## Related pages [#related-pages]

* [Pagination](https://www.prisma.io/docs/orm/prisma-client/queries/pagination)
* [Select fields](https://www.prisma.io/docs/orm/prisma-client/queries/select-fields)
* [Prisma Client API reference](https://www.prisma.io/docs/orm/reference/prisma-client-reference#filter-conditions-and-operators)

## Related pages

- [`Aggregation, grouping, and summarizing`](https://www.prisma.io/docs/orm/prisma-client/queries/aggregation-grouping-summarizing): Use Prisma Client to aggregate, group by, count, and select distinct.
- [`CRUD`](https://www.prisma.io/docs/orm/prisma-client/queries/crud): Learn how to perform create, read, update, and delete operations
- [`Excluding fields`](https://www.prisma.io/docs/orm/prisma-client/queries/excluding-fields): Learn how to exclude fields from Prisma Client results with the omit option.
- [`Full-text search`](https://www.prisma.io/docs/orm/prisma-client/queries/full-text-search): Learn how to search text fields with Prisma Client using your database's native full-text search support.
- [`Pagination`](https://www.prisma.io/docs/orm/prisma-client/queries/pagination): Learn how to paginate Prisma Client query results with offset pagination and cursor-based pagination.