LIKE and Pattern Matching

Estimated reading: 2 minutes 15 views

1. What is LIKE in SQL?

The LIKE operator in SQL is used to search for a specified pattern in a column. It is commonly used in the WHERE clause to filter results based on partial string matches.


2. What is a wildcard in SQL?

Wildcards are special characters used with the LIKE operator to enable pattern matching. The two main wildcards in SQL are:

  • %: Represents any sequence of characters (including no characters).
  • _: Represents exactly one character.

3. Write a query to find records where a column value starts with ‘A’.

To match values that start with a specific character or string, use the LIKE operator with the % wildcard at the end.

Example:

				
					SELECT name 
FROM employees 
WHERE name LIKE 'A%';

				
			

4. How do you match rows where a value ends with a specific pattern using LIKE?

To match values that end with a specific pattern, use the LIKE operator with the % wildcard at the beginning.

Example:

				
					SELECT name 
FROM employees 
WHERE name LIKE '%son';

				
			

5. Explain how the % and _ wildcards work in SQL.

  • %: Matches any sequence of characters (including no characters).
  • _: Matches exactly one character.

Examples:

  • LIKE 'A%': Matches ‘A’, ‘Abc’, ‘Apple’, etc.
  • `LIKE ‘a‘: Matches ‘cat’, ‘bat’, etc. (values with exactly 3 characters where the middle one is ‘a’).

Leave a Comment

Share this Doc

LIKE and Pattern Matching

Or copy link

CONTENTS