The SQL LIKE operator is used for pattern matching in searchqueries. It allows you to search for data in a column that matches a specified pattern, which can include wildcard characters. The two main wildcard characters used with the
LIKE operator are % and _.
Here's how the LIKE operator works:
%(Percentage Sign): This wildcard represents any sequence of characters (including zero characters).
_ (Underscore): This wildcard represents a single character.
Here are some common uses of the LIKE operator for pattern matching in search queries:
Using % for Wildcard Matching:
To find values that start with a specific pattern, use % at the end of the pattern.
To find values that end with a specific pattern, use % at the beginning of the pattern.
To find values that contain a specific pattern anywhere within them, use
% at both ends of the pattern.
Example:
-- Find all employees whose names start with "John"
SELECT * FROM employees WHERE name LIKE 'John%';
-- Find all products with "Widget" anywhere in their name
SELECT * FROM products WHERE product_name LIKE '%Widget%';
Using _ for Single-Character Matching:
To find values with a specific character at a particular position, use
_ in place of that character.
Example:
-- Find all two-letter country codes where the second letter is "A"
SELECT * FROM countries WHERE country_code LIKE '_A%';
The LIKE operator is versatile and allows you to perform flexible pattern matching searches in SQL. It's especially useful when you need to search for data based on partial or approximate patterns in text or string columns.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The SQL LIKE operator is used for pattern matching in search queries. It allows you to search for data in a column that matches a specified pattern, which can include wildcard characters. The two main wildcard characters used with the LIKE operator are % and _.
Here's how the LIKE operator works:
Here are some common uses of the LIKE operator for pattern matching in search queries:
Using % for Wildcard Matching:
Using _ for Single-Character Matching:
The LIKE operator is versatile and allows you to perform flexible pattern matching searches in SQL. It's especially useful when you need to search for data based on partial or approximate patterns in text or string columns.