# Relation filters

> Filter rows by their related rows with is, isNot, some, every, and none.

URL: https://better-drizzle.com/docs/querying/relation-filters

Use relation filters when the rows you want depend on their related rows: users with at least one published post, posts whose author is active. They compile to `EXISTS` subqueries, so no rows are joined into the result. Loading related rows is covered in [relations](/docs/querying/relations).

`where` can constrain a row by its relations. The operators depend on the relation's cardinality.

## To-one relations: `is` / `isNot`

For a "belongs to" / "has one" relation, use `is` (and `isNot`) with a nested filter:



**better-drizzle**

```ts  
const posts = await client.posts.findMany({
	where: {
		author: {
			is: { active: true },
		},
	},
	include: { author: true },
});
```

**Drizzle**

```ts  
import { eq, getColumns } from 'drizzle-orm';

const activeAuthorPosts = await db
	.select({ ...getColumns(posts), author: getColumns(users) })
	.from(posts)
	.innerJoin(users, eq(posts.authorId, users.id))
	.where(eq(users.active, true));
```

## To-many relations: `some` / `every` / `none`

For a "has many" relation, choose the quantifier:



**better-drizzle**

```ts  
// Users who have at least one published post
const authors = await client.users.findMany({
	where: {
		posts: { some: { published: true } },
	},
});

// Users with no unpublished posts
const fullyPublished = await client.users.findMany({
	where: {
		posts: { none: { published: false } },
	},
});
```

**Drizzle**

```ts  
import { and, eq, exists, notExists } from 'drizzle-orm';

// Users who have at least one published post
const authors = await db
	.select()
	.from(users)
	.where(
		exists(
			db
				.select()
				.from(posts)
				.where(and(eq(posts.authorId, users.id), eq(posts.published, true))),
		),
	);

// Users with no unpublished posts
const fullyPublished = await db
	.select()
	.from(users)
	.where(
		notExists(
			db
				.select()
				.from(posts)
				.where(and(eq(posts.authorId, users.id), eq(posts.published, false))),
		),
	);
```

| Operator | True when…                            |
| -------- | ------------------------------------- |
| `some`   | at least one related row matches      |
| `every`  | all related rows match                |
| `none`   | no related row matches                |
| `is`     | the single related row matches        |
| `isNot`  | the single related row does not match |

## Combining relation and scalar filters

Relation filters compose with scalar filters and logical operators like anything else:



**better-drizzle**

```ts  
const posts = await client.posts.findMany({
	where: {
		AND: [
			{ published: true },
			{
				author: {
					is: { email: { endsWith: '@company.com' } },
				},
			},
		],
	},
	select: {
		id: true,
		title: true,
		author: { select: { id: true, email: true } },
	},
});
```

**Drizzle**

```ts  
import { and, eq, like } from 'drizzle-orm';

const companyPosts = await db
	.select({
		id: posts.id,
		title: posts.title,
		author: { id: users.id, email: users.email },
	})
	.from(posts)
	.innerJoin(users, eq(posts.authorId, users.id))
	.where(and(eq(posts.published, true), like(users.email, '%@company.com')));
```
