How To Use Like In Sql in SQL
How to Use LIKE in SQL
The LIKE operator is used to search for a specified pattern in a column, often with wildcards.
Syntax:
SELECT columnsFROM table_nameWHERE column_name LIKE pattern;Wildcards used with LIKE:
| Wildcard | Meaning | Example |
|---|---|---|
% | Matches zero or more characters | 'Jo%' matches "John", "Joanna" |
_ | Matches exactly one character | 'J_n' matches "Jan", "Jen" |
Examples:
1. Find employees whose name starts with "Jo":
SELECT *FROM employeesWHERE name LIKE 'Jo%';2. Find employees whose name has "an" in the middle:
SELECT *FROM employeesWHERE name LIKE '%an%';3. Find employees whose name is 3 characters and starts with "J":
SELECT *FROM employeesWHERE name LIKE 'J__';Notes:
LIKEis case-insensitive in some databases (e.g., MySQL), but case-sensitive in others (e.g., PostgreSQL).To perform case-insensitive search in PostgreSQL, use
ILIKE.
If you want examples with escape characters or regex alternatives, just ask!