What is the LIKE Operator?
The LIKE operator is used in SQL to search for a specific pattern in a column. It allows you to match text using wildcards instead of exact values.
Syntax
SELECT column1, column2
FROM table_name
WHERE column_name LIKE pattern;
column_name– The column you want to search inpattern– The text pattern to match
Wildcards Used with LIKE
- % – Represents zero, one, or multiple characters
- Example:
'A%'matches any value starting with A - Example:
'%son'matches any value ending with son
- Example:
- _ – Represents a single character
- Example:
'A_n'matches ‘Ann’, ‘Aen’, etc.
- Example:
Examples
Example 1: Find names starting with “J”
SELECT name
FROM students
WHERE name LIKE 'J%';
Example 2: Find emails ending with “@gmail.com”
SELECT email
FROM users
WHERE email LIKE '%@gmail.com';
Example 3: Find words with “ar” in the middle
SELECT product_name
FROM products
WHERE product_name LIKE '%ar%';
Example 4: Find three-letter names starting with “A”
SELECT name
FROM employees
WHERE name LIKE 'A__';
Key Points to Remember
- LIKE is case-insensitive in some databases and case-sensitive in others.
%can be used at the beginning, end, or both sides of the pattern._is useful when you want to match a single unknown character.- For exact matches, use
=instead of LIKE.