PostgreSQL wildcard LIKE for any of a list of words

Cover Image for PostgreSQL wildcard LIKE for any of a list of words
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

PostgreSQL Wildcard LIKE: Finding Rows with Any of a List of Words

So you have a PostgreSQL database with a table that has a varchar field, and you want to find any rows that contain any of the words from a given list. You've already tried using multiple like conditions, but you're looking for a more elegant solution. Well, you're in luck! In this blog post, we'll explore a few easy solutions to this problem, so let's get started. 💪

The Not-So-Elegant Solution

Before we dive into the more elegant solutions, let's quickly recap the not-so-elegant approach you've already tried:

select *
from table
where (lower(value) like '%foo%' or lower(value) like '%bar%' or lower(value) like '%baz%')

While this approach gets the job done, it can quickly become cumbersome and repetitive, especially if you have a long list of words. Plus, it's not the most efficient solution either.

Solution 1: Using the ANY Operator with an Array

One of the more elegant solutions to this problem involves using the ANY operator along with an array. Here's how you can rewrite the query using this approach:

select *
from table
where lower(value) like any (array['%foo%', '%bar%', '%baz%'])

By using the ANY operator in this way, you can provide a list of patterns as an array and check if any of them match the value column. This approach is not only more concise but also improves the query's performance by avoiding repeated like conditions.

Solution 2: Regular Expressions with the ~ Operator

Another way to tackle this problem is by using regular expressions. PostgreSQL offers powerful regex capabilities, making it a great option for pattern matching. Here's how you can rewrite the query using the ~ operator:

select *
from table
where lower(value) ~ 'foo|bar|baz'

The ~ operator allows you to match a column against a regular expression pattern. In this case, we're using the pipe (|) character to signify an OR operation, matching any row that contains either "foo," "bar," or "baz." This approach can be especially useful if you need more advanced pattern matching options.

Take It to the Next Level with Indexing

Now that you have some elegant solutions to your original problem, let's take it a step further! To optimize the query's performance, consider adding an index on the value column. This will significantly speed up the search operation, particularly if you have a large table.

create index value_idx on table (lower(value));

By creating an index on the lowercase version of the value column, you can dramatically improve the search speed for your wildcard matching queries.

Conclusion

There you have it! Finding rows in PostgreSQL that contain any of the words from a list can be elegantly achieved using the ANY operator with an array or regular expressions with the ~ operator. Don't forget to take advantage of indexing to further boost the query performance. 🚀

Now it's your turn. Have you come across any other creative solutions for this problem? Let us know in the comments below! And if you found this post helpful, don't forget to share it with your fellow PostgreSQL enthusiasts. Happy querying! 😊


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello