Full-Text Search in SQL

When working with relational databases, efficient searching is crucial. Traditional methods, such as pattern matching, have limitations regarding complex queries or large datasets. Instead, we can use full-text search (FTS), a powerful feature in SQL databases designed to enhance search capabilities. Let’s explore how we can implement FTS, its benefits, and its usage.

What is full-text search?

Full-text search is a specialized query method employed in SQL databases to look for words or phrases within text columns. FTS goes beyond traditional pattern matching using the LIKE operator, considering the meaning and context of words in a document. This makes it particularly useful for applications like document searching, content management systems, and e-commerce platforms.

Enabling full-text search

Before we dive into the utility of full-text search, it’s important to ensure that FTS is enabled on our SQL server. Most modern SQL databases, such as Microsoft SQL Server, MySQL, and PostgreSQL, offer support for FTS.

In a MySQL Server, for instance, we can create a full-text index on a table column like this:

main.sql
db.sql
CREATE FULLTEXT INDEX idx_SalesPerson
ON SalesData(SalesPerson);
ALTER TABLE SalesData ENGINE=InnoDB;

Basic full-text search queries

Now, let’s explore some basic FTS queries using the MATCH and AGAINST keywords. Consider a table named SalesData with a full-text index on the SalesPerson column.

main.sql
db.sql
SELECT * FROM SalesData
WHERE MATCH(SalesPerson) AGAINST('William');

This query retrieves all rows from the SalesData table where the SalesPerson column contains the specified search term 'William'.

Advanced full-text search

FTS supports more advanced features to refine search results. For instance, the IN BOOLEAN MODE modifier allows for boolean searches. This means that we can set multiple parameters for our query in the same statement using the AND, OR, or NOT operators.

main.sql
db.sql
SELECT * FROM SalesData
WHERE MATCH(SalesPerson) AGAINST('John AND Alice' IN BOOLEAN MODE);

This query retrieves the SalesPersons containing the words 'John' and 'Alice'.

Full-text search with relevance scores

SQL also provides relevance scoresScores that determine the match strength of a specific row based on the provided parameters. for FTS results, allowing us to rank them based on their match strength.

main.sql
db.sql
SELECT * FROM SalesData
WHERE MATCH(SalesPerson) AGAINST('William')
ORDER BY MATCH(SalesPerson) AGAINST('John') DESC;

This query fetches the SalesPersons containing the term 'William' and orders them by giving higher score to rows containing 'John' using the ORDER BY expression.

Conclusion

Full-text search is a robust feature that significantly improves the search capabilities of SQL databases. By considering the meaning and context of words, it opens up new possibilities for applications requiring sophisticated search functionality. Whether we’re building a content management system or enhancing search in our application, FTS is a tool worth mastering for precision and efficiency.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved