To filter rows in an SQL SELECT statement when a text column contains specific words, use LIKE with % wildcards for simple substring or word-boundary patterns, REGEXP / SIMILAR TO / regex operators for true whole-word matching, or full-text search when searching large text columns efficiently. A plain word match finds substrings inside other words (for example, "cat" matches "category" or "concatenate"), so word boundaries matter when you want exact words.
1. Basic Substring Match with LIKE
Use % before and after a word to match rows where the column contains that sequence of characters anywhere in the string.
SELECT article_id, title
FROM articles
WHERE title LIKE '%python%';
- Case sensitivity depends on the database and column collation (for example,
ILIKEis case-insensitive in PostgreSQL, while MySQL and SQL Server often depend on collation). - To avoid false matches inside other words, include spaces or punctuation around the word in your pattern when text is consistently spaced:
SELECT article_id, title
FROM articles
WHERE (' ' || title || ' ') LIKE '% python %';
Note: String concatenation differs by SQL dialect (
||in PostgreSQL/SQLite,+in SQL Server,CONCAT(' ', title, ' ')in MySQL).
2. Matching Multiple Specific Words (AND / OR)
Combine conditions with AND when all target words must appear, or with OR when any target word can appear. Always group OR clauses in parentheses when mixing AND and OR.
-- Must contain BOTH "sql" AND "join"
SELECT post_id, body
FROM posts
WHERE body LIKE '%sql%'
AND body LIKE '%join%';
-- Must contain "python" AND either "list" OR "tuple"
SELECT post_id, body
FROM posts
WHERE body LIKE '%python%'
AND (body LIKE '%list%' OR body LIKE '%tuple%');
3. True Whole-Word Matching with Regular Expressions
Regular expressions support word boundaries so you match the standalone word rather than substrings inside longer words.
PostgreSQL (~ case-sensitive, ~* case-insensitive)
SELECT post_id, body
FROM posts
WHERE body ~* '\ypython\y';
MySQL (REGEXP)
SELECT post_id, body
FROM posts
WHERE body REGEXP '[[:<:]]python[[:>:]]';
(In MySQL 8.0+, word-boundary regex support varies by ICU version; (?i)\bpython\b is commonly used where supported.)
4. Full-Text Search for Large Text Columns
Wildcard LIKE '%word%' scans rows and cannot typically use a standard B-tree index efficiently on leading wildcards. For large text fields or natural-language word searches, use your database’s full-text search feature:
- MySQL:
MATCH(column_name) AGAINST('word1 word2' IN BOOLEAN MODE)with aFULLTEXTindex. - PostgreSQL:
to_tsvector('english', column_name) @@ to_tsquery('english', 'word1 & word2')with aGINindex. - SQL Server:
CONTAINS(column_name, '"word1" AND "word2"')with a Full-Text Index.
Quick Checklist for Word Filtering
- Decide between substring vs. whole word:
catmatches"education"; word boundaries or full-text search target standalone words. - Check case sensitivity: Test whether your database or column collation is case-sensitive before relying on
LIKE. - Watch performance on big tables: Leading
%wildcards can slow down large tables; consider full-text indexing for heavy text search.
