What Is SQL CONTAINS?

SQL CONTAINS is a full-text search predicate that checks whether a column indexed for full-text search includes a given word or phrase. Use it in SQL Server and some other engines to perform fast linguistic searches instead of pattern-matching with LIKE. Always pair it with a full-text index.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SQL CONTAINS performs full-text searches on columns with a full-text index, returning rows that include the specified word, phrase, prefix, or boolean expression. Faster and more flexible than LIKE, it supports inflectional forms and proximity searches when the database engine provides a full-text catalog.

What Is SQL CONTAINS?

SQL CONTAINS is a full-text predicate that returns true when a full-text indexed column contains a specified word, phrase, or prefix. It is available in SQL Server, Azure SQL, and Oracle (via CONTEXT index) but not in MySQL or PostgreSQL core.

Why Use CONTAINS Instead of LIKE?

CONTAINS leverages an inverted index, yielding millisecond search times on large text compared to LIKE, which scans every row. CONTAINS also supports linguistic searches—handling plurals, synonyms, and proximity—providing richer search semantics than simple wildcard matching.

How Does CONTAINS Work Internally?

The database builds a full-text catalog that stores each term once and maps it to row identifiers. CONTAINS queries use this catalog to locate matching rows without table scans, improving performance as data size grows.

What Is Required Before Using CONTAINS?

You must create a full-text catalog and then enable a full-text index on the target table and column. Without the index, CONTAINS throws an error. Proper permissions to manage full-text objects are also necessary.

How Do I Create a Full-Text Index?

Create the catalog with CREATE FULLTEXT CATALOG, then build the index using CREATE FULLTEXT INDEX ON table(column) KEY INDEX pk_name. After population, CONTAINS queries become available.

How Do I Search for a Single Word?

Use CONTAINS(column, 'word') in the WHERE clause. The engine returns rows whose tokenized text includes the word in any position or capitalization.

How Do I Search for a Phrase?

Wrap the phrase in double quotes inside the predicate: CONTAINS(column, '"exact phrase"'). Only rows containing that contiguous phrase are returned.

How Do I Perform Prefix Searches?

Add an asterisk after the prefix: CONTAINS(column, 'prefix*'). The predicate matches any term beginning with the prefix, enabling auto-complete–style queries.

Can I Combine Words with AND/OR/NOT?

Yes. Use boolean operators inside the CONTAINS string: 'word1 AND word2', 'word1 OR word2', 'word1 AND NOT word3'. Parentheses control precedence for complex expressions.

How Do Proximity Searches Work?

In SQL Server, use NEAR: CONTAINS(column, 'NEAR(("data", "science"))'). NEAR returns rows where terms appear within 50 words by default or a custom distance if specified, improving contextual relevance.

Can I Weight Terms for Ranking?

Full-text search returns a relevance score via CONTAINSTABLE, allowing you to rank rows by importance. Specify weight with ISABOUT: 'ISABOUT(word1 WEIGHT(0.8), word2 WEIGHT(0.2))' to bias scoring.

How Do I Filter by Language?

Specify LANGUAGE 1033 (for English) or another LCID in the query: CONTAINS(column, 'FORMSOF(INFLECTIONAL, run)', LANGUAGE 1033). This ensures stemming and thesaurus rules follow the chosen language.

What Security Considerations Exist?

CONTAINS respects row-level permissions, but full-text catalogs live in the file system. Ensure correct NTFS permissions and protect backups containing catalogs to avoid data leakage.

How Do I Maintain Full-Text Indexes?

Schedule automatic population or incremental updates after bulk loads. Monitor fragmentation and rebuild indexes during off-peak hours to sustain query speed.

Is CONTAINS Part of Standard SQL?

No. CONTAINS is vendor-specific. SQL Server and Oracle use similar syntax, whereas PostgreSQL offers the @@ operator with tsvector, and MySQL uses MATCH…AGAINST. Always check engine documentation.

What Are Alternatives in Other Databases?

Use PostgreSQL full-text search (tsvector @@ to_tsquery), MySQL’s FULLTEXT index with MATCH…AGAINST, or SQLite FTS5 extension. Logic mirrors CONTAINS but syntax differs.

What Are Best Practices for Using CONTAINS?

Create indexes only on columns that truly need searching; avoid blobs with irrelevant text. Keep the catalog on fast storage. Update statistics regularly, and monitor query plans for forced table scans.

What Are the Key Takeaways?

SQL CONTAINS delivers fast, flexible full-text search when backed by proper indexing. Use phrases, prefixes, boolean logic, and proximity to refine results. Maintain indexes and understand engine-specific syntax for optimal performance.

Frequently Asked Questions (FAQs)

Does SQL Server automatically update full-text indexes?

Yes, if change tracking is set to AUTO. The engine schedules background population after every DML operation. Use MANUAL for bulk-load scenarios and trigger updates explicitly.

How large can a full-text catalog grow?

Catalog size depends on vocabulary, not raw data size. Expect 10–20% of the original text volume on English datasets, but monitor disk usage and plan storage accordingly.

Can I search multiple columns with CONTAINS?

Yes. Specify a list inside parentheses: CONTAINS((Title, Summary), 'AI'). All listed columns must be part of the same full-text index.

Is CONTAINS case-sensitive?

No in most collations. Full-text search uses linguistic analysis, making searches case-insensitive by default. Specify a binary collation for case-sensitive requirements.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo