Skip to main content
Version: v2 RC

Using Filters

Filters allow you to only return rows that match certain conditions.

Filters can be used on select(), update(), upsert(), and delete() queries.

If a Postgres function returns a table response, you can also apply filters.

Applying Filters

Filters must be applied after any of select(), update(), upsert(), delete(), and rpc() and before modifiers.

const { data, error } = await supabase
.from('cities')
.select('name, country_id')
.eq('name', 'The Shire') // Correct

const { data, error } = await supabase
.from('cities')
.eq('name', 'The Shire') // Incorrect
.select('name, country_id')

Chaining

Filters can be chained together to produce advanced queries. For example, to query cities with population between 1,000 and 10,000:

const { data, error } = await supabase
.from('cities')
.select('name, country_id')
.gte('population', 1000)
.lt('population', 10000)

Conditional Chaining

Filters can be built up one step at a time and then executed. For example:

const filterByName = null
const filterPopLow = 1000
const filterPopHigh = 10000

let query = supabase
.from('cities')
.select('name, country_id')

if (filterByName) { query = query.eq('name', filterByName) }
if (filterPopLow) { query = query.gte('population', filterPopLow) }
if (filterPopHigh) { query = query.lt('population', filterPopHigh) }

const { data, error } = await query

Filter by values within a JSON column

const { data, error } = await supabase
.from('users')
.select()
.eq('address->postcode', 90210)

Filter Foreign Tables

You can filter on foreign tables in your select() query using dot notation:

const { data, error } = await supabase
.from('countries')
.select(`
name,
cities!inner (
name
)
`)
.eq('cities.name', 'Bali')