Open In App

SQL SELECT WHERE Field Contains Words

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The SQL SELECT WHERE field Contains Words” in SQL is used to find data with specific words in it. It helps us to find patterns in text fields. One more way to do this is by using full-text search, which searches entire blocks of text to find what we need.

In simple words, we can say SQL gives us ways to find data based on words or phrases in fields which makes it easier to get the required information.

SQL SELECT WHERE Clause

Using the SELECT WHERE clause, we can filter a query based on certain conditions. For example, if we want to filter records by word matches, we use the LIKE operator and wildcard characters.

Syntax:

SELECT column1, column2, …

FROM table_name

WHERE condition;

Explanation: This query is used for searching data where the condition is true in a mentioned column of a table.

‘LIKE’ operator

The ‘LIKE’ operator is commonly used for matching patterns in SQL queries. We can use ‘%’ as a wildcard to represent any sequence of characters.

Syntax:

SELECT column1, column2, …FROM table_nameWHERE column_name LIKE ‘%word%’;

The ‘%word%’ will match any value that contains the word ‘word’ and the ‘%’ before and after makes sure that the mentioned word can occur at any position(beginning, middle or end).

Example of SQL SELECT WHERE Field Contains Words

To search for a record that contains a specific word in a specific field, we will use SELECT statement in SQL with WHERE clause to filter the results based on a specific word.

Consider a table named products with columns product_id and product_name. We want to select products where the product name contains the word “chair“.

Query:

CREATE TABLE products (
product_id INT,
product_name VARCHAR(100)
);


INSERT INTO products (product_id, product_name) VALUES
(1, 'Office Chair'),
(2, 'Dining Chair'),
(3, 'Desk Lamp'),
(4, 'Armchair');
SELECT * FROM PRODUCTS;

Output:

sql_table_for_products

The table created using create command in sql

We search for the word ‘chair using 2 approaches, discussed below:

  • Using LIKE Operator with wildcards
  • Using Full taxt search (MATCH .. .AGAINST)

Approach 1: Using LIKE Operator with Wildcards

The LIKE operator is used to match pattern within a field. The ‘%’ wildcard represents any sequence of characters, which help us to search for a specific word in the field.

Syntax:

SELECT * FROM your_table_name WHERE your_column_name LIKE ‘%your_word%’;

Example

Using LIKE Operator to with SELECT WHERE to find the words present in a specific colum.

Step 1: Basic SQL SELECT statement without any conditions.

SELECT * FROM products;

Output:

sql_table_for_products

product table

Step 2: SQL SELECT statement using the LIKE operator to filter rows where the product name contains the word “chair”.

SELECT * FROM products WHERE product_name LIKE '%chair%';

Output:

sql-output

sql select where field contains words

Explanation:

The query retrieves all records from the “products” table where the “product_name” contains the word “chair.” The output includes all rows with product names that have “chair” as a substring, providing a filtered result set.

Approach 2: Using Full-Text Search

Full-text is an advanced way to search text in table. First we will create a full-text index on column(s) we want to search then we will use MATCH AGAINST keyword to search the data.

A full-text index is a special type of index that help us for efficient searching of text-based data. By creating a full-text index on a column, we enable the database to perform fast text searches on that column.

The MATCH(your_column_name) clause indicates that we are performing a full-text search on the specified column. The AGAINST (‘your_word’) clause specifies the search term or terms to match against. In this case, it’s your_word.

When we use MATCH AGAINST for full-text search, the search term can be a single word, multiple words, or even a phrase.

Syntax:

//Create a Full-Text Index:CREATE FULLTEXT INDEX idx_your_index_name ON your_table_name(your_column_name);//Use MATCH AGAINST Syntax in SELECT statement to search for specific words:SELECT * FROM your_table_name WHERE MATCH(your_column_name) AGAINST (‘your_word’);

Example

Step 1: Create a Full-Text Index.

CREATE FULLTEXT INDEX idx_product_name ON products(product_name);

Step 2: Use MATCH AGAINST Syntax in SELECT statement to search for specific words.

SELECT * FROM products WHERE MATCH(product_name) AGAINST ('chair');

Output:

sql-output

sql select where match against

Explanation:

The query uses full-text search to retrieve records from the “products” table where the “it ” matches the word “chair.” The output includes rows with product names containing variations or forms of the specified word, providing a comprehensive search result.

Conclusion

In conclusion, using the SELECT WHERE clause to filter records by word matches is a powerful way to get accurate information. Whether you use the LIKE operator or you use full-text searching, knowing how to use these techniques will help you get the information you need faster, making your database queries more efficient.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads