LIKE vs CONTAINS: Full-Text Search in SQL Server Explained Simply

Almost every business app has a search box, and almost every search box starts life as this:

SELECT * FROM Products WHERE Description LIKE '%' + @Search + '%';

It works perfectly with 500 products. With 2 million rows it takes seconds, it can’t find “running” when the user types “run”, and it can’t rank the best matches first. That’s when people discover full-text search and the CONTAINS predicate.

This guide compares LIKE and full-text search in SQL Server in plain English: why LIKE '%word%' is slow, how to set up a full-text index, how to use CONTAINS, FREETEXT and ranking, and when LIKE is still the better choice. There’s a MySQL section at the end.

Comparison of LIKE with a leading wildcard, which reads every row and matches characters, and full-text CONTAINS, which looks up words, prefixes and word forms and can rank results

The sample data

CREATE TABLE dbo.Products (
    ProductID   INT          NOT NULL CONSTRAINT PK_Products PRIMARY KEY,
    Name        VARCHAR(100) NOT NULL,
    Description VARCHAR(500) NOT NULL
);

INSERT INTO dbo.Products VALUES
(1, 'Wireless Keyboard',  'Slim wireless keyboard with quiet keys for the office'),
(2, 'USB Keyboard',       'Wired keyboard with a numeric keypad'),
(3, 'Wireless Mouse',     'Ergonomic wireless mouse, runs on one AA battery'),
(4, 'Keyboard Cover',     'Silicone cover that protects your keyboard from dust'),
(5, 'Laptop Stand',       'Aluminium stand that raises the screen to eye level'),
(6, 'Running Headphones', 'Wireless headphones that stay in place while running');

How LIKE works, and why it gets slow

LIKE compares text character by character against a pattern. % means “any characters” and _ means “exactly one character” (see SQL LIKE, IN and BETWEEN Explained Simply for the basics).

Whether an index helps depends on where the % is:

PatternCan use an index seek?Why
LIKE 'Key%'YesThe start is known, so SQL Server can jump to “Key” in a sorted index, like a phone book
LIKE '%board'NoAny value could end with “board”
LIKE '%board%'NoAny value could contain it

With a leading %, SQL Server has to read every row and test the pattern on each one: an index scan or table scan (the difference is explained in Index Seek vs Index Scan). The cost grows in a straight line with the table. On a large description column, that’s slow and uses a lot of CPU.

LIKE also only matches exact characters:

  • LIKE '%run%' finds “runs” and “running” (they contain “run”), but also “brunch” and “prune”.
  • It can’t search for “keyboard and wireless in any order” without two conditions.
  • It can’t say which row is the best match.

What full-text search does differently

A full-text index works like the index at the back of a book. SQL Server splits each text value into words (using a language-aware word breaker), and stores a list of which rows contain each word.

A search for “keyboard” then looks the word up in that list and gets rows 1, 2 and 4 straight away, without reading the descriptions. Because it understands words, it can also:

  • match word forms: “run” finds “runs” and “running”;
  • match prefixes: "key*" finds keyboard, keys and keypad;
  • combine words with AND, OR, NOT and NEAR;
  • rank results by how well they match;
  • ignore very common stopwords like “the” and “with”.

The trade-off: it works on whole words. A full-text search for “board” does not find “keyboard”, while LIKE '%board%' does.

Setting up a full-text index

Full-text search is an optional SQL Server feature. Check it’s installed:

SELECT FULLTEXTSERVICEPROPERTY('IsFullTextInstalled');  -- 1 = installed

It’s included in Standard, Enterprise and Developer editions, in Express with Advanced Services, and in Azure SQL Database. If it returns 0, add the “Full-Text and Semantic Extractions for Search” feature with SQL Server Setup.

Then two statements:

CREATE FULLTEXT CATALOG ftProducts AS DEFAULT;

CREATE FULLTEXT INDEX ON dbo.Products (Name, Description)
    KEY INDEX PK_Products
    WITH CHANGE_TRACKING AUTO;
  • The catalog is just a container for full-text indexes.
  • KEY INDEX must be a unique, single-column, non-nullable index on the table, usually the primary key.
  • CHANGE_TRACKING AUTO keeps the index updated when rows change. Updates are applied in the background, so a brand-new product can take a few seconds to appear in search results.
  • You can have only one full-text index per table, but it can cover several columns.

Searching with CONTAINS

A single word

SELECT ProductID, Name
FROM dbo.Products
WHERE CONTAINS(Description, 'keyboard');

Returns 1, 2 and 4. Search all indexed columns with *:

WHERE CONTAINS(*, 'wireless')   -- rows 1, 3 and 6 (it's in the Name or Description)

AND, OR, NOT

WHERE CONTAINS(*, 'wireless AND keyboard')       -- 1
WHERE CONTAINS(*, 'mouse OR headphones')         -- 3, 6
WHERE CONTAINS(*, 'wireless AND NOT keyboard')   -- 3, 6

Prefix search

Put the term in double quotes with *:

WHERE CONTAINS(*, '"key*"')   -- keyboard, keys, keypad: rows 1, 2, 4

Without the double quotes, the * is treated as part of the word.

Word forms

WHERE CONTAINS(Description, 'FORMSOF(INFLECTIONAL, run)')   -- runs (3), running (6)

INFLECTIONAL matches plurals and verb tenses of the word in the index’s language.

Words close together

WHERE CONTAINS(Description, 'NEAR((wireless, keyboard), 3)')

Finds rows where the two words appear within 3 words of each other.

A search term from a parameter

Users type plain words, not CONTAINS syntax. Build a safe search condition from the input instead of passing it straight through (a stray quote or the word “AND” would cause a syntax error):

DECLARE @UserInput NVARCHAR(100) = N'wireless keyb';

-- Turn each word into a prefix term and join them with AND:  "wireless*" AND "keyb*"
DECLARE @Search NVARCHAR(400) =
    (SELECT STRING_AGG('"' + REPLACE(value, '"', '') + '*"', ' AND ')
     FROM STRING_SPLIT(@UserInput, ' ')
     WHERE value <> '');

SELECT ProductID, Name
FROM dbo.Products
WHERE CONTAINS(*, @Search);

Always pass it as a parameter, never by concatenating it into a SQL string (SQL injection works here too). STRING_AGG needs SQL Server 2017 or later.

FREETEXT: “find things about this”

FREETEXT takes a phrase as a user would type it, breaks it into words, matches their forms, and returns rows that match any of them:

SELECT ProductID, Name
FROM dbo.Products
WHERE FREETEXT(Description, 'quiet keyboards for running');

It’s looser than CONTAINS: good for “help me find something”, less good when the user wants exact results.

Ranking the best matches first

CONTAINSTABLE and FREETEXTTABLE return a table with a RANK column (0 to 1000). Join it back to your table:

SELECT p.ProductID, p.Name, ft.[RANK]
FROM CONTAINSTABLE(dbo.Products, *, 'wireless OR keyboard') AS ft
JOIN dbo.Products AS p ON p.ProductID = ft.[KEY]
ORDER BY ft.[RANK] DESC;

The “Wireless Keyboard” row, which matches both words, comes first. The exact numbers depend on the whole table, so treat them as a sort order, not a score to show users. Add a fourth argument (top_n_by_rank) to return only the best few hundred matches.

When LIKE is still the right choice

Full-text search isn’t always better. Keep LIKE when:

  • The table is small (a few thousand rows). The scan is fast enough and you avoid extra setup.
  • The search is “starts with”: product codes, invoice numbers, customer names typed into a lookup. LIKE 'INV-2026%' with a normal index is a fast seek.
  • You need substring matches inside words: part numbers like AB-4471-X, where users type 4471. Full-text won’t find part of a token.
  • The column is short codes, not language.

A common pattern is both: LIKE 'term%' on codes and names for a quick lookup box, and full-text on descriptions and notes for a proper search page.

MySQL: FULLTEXT and MATCH … AGAINST

MySQL (InnoDB, 5.6 and later) has built-in full-text indexes:

ALTER TABLE Products ADD FULLTEXT INDEX ft_products (Name, Description);

-- Natural language mode: like FREETEXT, ranked by relevance
SELECT ProductID, Name,
       MATCH(Name, Description) AGAINST('wireless keyboard') AS Score
FROM Products
WHERE MATCH(Name, Description) AGAINST('wireless keyboard')
ORDER BY Score DESC;

-- Boolean mode: + must contain, - must not, * prefix
SELECT ProductID, Name
FROM Products
WHERE MATCH(Name, Description) AGAINST('+wireless -keyboard' IN BOOLEAN MODE);   -- 3, 6

SELECT ProductID, Name
FROM Products
WHERE MATCH(Name, Description) AGAINST('key*' IN BOOLEAN MODE);                  -- 1, 2, 4

The first query returns the Wireless Keyboard first, then the other rows that mention either word. A few MySQL details:

  • The column list in MATCH() must be exactly the columns of one FULLTEXT index.
  • InnoDB ignores words shorter than 3 characters by default (innodb_ft_min_token_size), so “AA” isn’t indexed.
  • MySQL doesn’t match word forms like SQL Server’s FORMSOF; use prefix search (run*) instead.

In Laravel, whereFullText(['name', 'description'], $term) generates the MATCH ... AGAINST for you.

Common mistakes

  • Using LIKE '%term%' on a big text column and adding more indexes to fix it. A normal index can’t help a leading wildcard.
  • Expecting full-text to find parts of words (“board” in “keyboard”).
  • Passing user input straight into CONTAINS, so a quote or the word “OR” causes an error.
  • Forgetting the double quotes in a prefix search: '"key*"', not 'key*'.
  • Assuming new rows are searchable instantly. Change tracking is asynchronous.
  • Full-text on code columns where LIKE 'prefix%' with a normal index is simpler and faster.

Practice exercises

Exercise 1. Which of these can use a normal index seek on Name: (a) LIKE 'Wire%', (b) LIKE '%less', (c) LIKE 'W_reless%'?

Exercise 2. Write a CONTAINS query that returns products whose description has any word starting with “wire” but doesn’t mention “mouse”.

Exercise 3. A user searches the full-text index for “pad”. Will row 2 (“numeric keypad”) be returned? What search would find it?

Answers

Answer 1. (a) Yes: the start is fixed. (b) No: it starts with %. (c) Yes, partly: SQL Server can seek on the fixed leading “W” and then test the rest of the pattern, so it reads only names starting with W.

Answer 2.

SELECT ProductID, Name
FROM dbo.Products
WHERE CONTAINS(Description, '"wire*" AND NOT mouse');

Rows 1, 2 and 6 (“wireless” and “Wired” both start with “wire”; row 3 is excluded because it mentions mouse).

Answer 3. No. “keypad” is indexed as one word, and full-text search matches whole words or prefixes, not the end of a word. LIKE '%pad%' would find it; so would a prefix search for "keyp*".

Summary

  • LIKE 'abc%' can use an index; LIKE '%abc%' can’t, so it scans every row.
  • Full-text search indexes words, so CONTAINS is fast on large text columns and understands prefixes, word forms, AND/OR/NOT and NEAR.
  • Use CONTAINSTABLE / FREETEXTTABLE to rank the best matches first.
  • Full-text matches whole words; use LIKE for codes, “starts with” lookups and substrings inside words.
  • In MySQL, use a FULLTEXT index with MATCH ... AGAINST (natural language or boolean mode).

Free SQL book for beginners

Want the LIKE basics first? My free book, Simplify SQL for Absolute Beginners, builds every lesson on one practice database, with exercises and answers. LIKE, IN and BETWEEN are Chapter 5 of the full book, and Chapters 1–3 are free now as a PDF and EPUB.

Related articles

Scroll to Top