/supabase-tutorials

How to query with filters in Supabase?

Discover how to query and filter data in Supabase using our step-by-step JavaScript guide with clear code examples for eq, gt, and like filters.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to query with filters in Supabase?

 

Step 1: Set up your Supabase Account and Project

 

  1. Create a Supabase account by visiting the Supabase website.
  2. Once logged in, create a new project by clicking on "New Project".
  3. Enter the project details and click "Create Project". This will set up a new instance for you.

 

Step 2: Install Supabase Client

 

To query data from Supabase, you'll need to install the Supabase JavaScript client in your project.

First, make sure you have Node.js installed. Then, initialize a new Node.js project, if you haven't already:

npm init -y

Next, install the Supabase client library:

npm install @supabase/supabase-js

 

Step 3: Initialize the Supabase Client

 

After installation, you need to initialize the Supabase client using the URL and the public API key of your Supabase project.

const { createClient } = require('@supabase/supabase-js')

const supabaseUrl = 'https://xyzcompany.supabase.co'
const supabaseKey = 'public-anon-key'
const supabase = createClient(supabaseUrl, supabaseKey)

Replace 'https://xyzcompany.supabase.co' and 'public-anon-key' with your actual Supabase project URL and anonymous key found in your project's settings under "API".

 

Step 4: Query with Filters

 

Now, let's retrieve data with specific filters. Assume you have a table named customers.

To fetch all entries from the customers table:

async function getCustomers() {
  let { data, error } = await supabase
    .from('customers')
    .select('*')

  if (error) console.log('Error:', error)
  else console.log('Data:', data)
}

getCustomers()

To filter the customers where the age is greater than 30:

async function getCustomersAgeOver30() {
  let { data, error } = await supabase
    .from('customers')
    .select('*')
    .gt('age', 30)

  if (error) console.log('Error:', error)
  else console.log('Data:', data)
}

getCustomersAgeOver30()

 

Step 5: Use Additional Filter Options

 

Supabase provides various methods for filtering data. Some commonly used filters are:

  • eq: Equal to
  • neq: Not equal to
  • gte: Greater than or equal to
  • lte: Less than or equal to
  • like: Pattern matching
  • ilike: Case-insensitive pattern matching

Here's an example using some of these filters:

// Filter customers with first name 'John'
async function getCustomersNamedJohn() {
  let { data, error } = await supabase
    .from('customers')
    .select('*')
    .eq('first_name', 'John')

  if (error) console.log('Error:', error)
  else console.log('Data:', data)
}

getCustomersNamedJohn()

// Filter customers whose last name starts with 'Do'
async function getCustomersWithLastNameStartingWithDo() {
  let { data, error } = await supabase
    .from('customers')
    .select('*')
    .like('last_name', 'Do%')

  if (error) console.log('Error:', error)
  else console.log('Data:', data)
}

getCustomersWithLastNameStartingWithDo()

 

Step 6: Combine Multiple Filters

 

You can combine multiple filters to retrieve more specific data:

// Filter customers named 'John' with age greater than or equal to 25
async function getSpecificCustomers() {
  let { data, error } = await supabase
    .from('customers')
    .select('*')
    .eq('first_name', 'John')
    .gte('age', 25)

  if (error) console.log('Error:', error)
  else console.log('Data:', data)
}

getSpecificCustomers()

 

Conclusion

 

By following the steps above, you can effectively query and filter data in your Supabase database using the JavaScript client. The flexibility of the query filters allows you to tailor your data requests precisely to your needs.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022