Loading
SQL LIKE Operator
The LIKE operator in SQL is used to search for a specific pattern in a column's text data. It is commonly used with the WHERE clause to filter rows that match a pattern using wildcard characters.



Wildcards Used with LIKE

WildcardDescription
%Matches zero, one or multiple characters
_Matches exactly one character

These wildcards let you write flexible queries to find data based on patterns, rather than exact matches.



Syntax

SELECT column1, column2, ... FROM table_name WHERE column_name LIKE pattern;



Employee Table

IDEmpNameCitySalary
1TomABC7000
2EnnaXYZ5000
3GennyABC8000



Example: Using % Wildcard

Find employees whose city starts with 'A'.

SELECT * FROM employee WHERE City LIKE 'A%';

Output:

IDEmpNameCitySalary
1TomABC7000
3GennyABC8000

Explanation: A% matches all cities that begin with the letter 'A', followed by any characters.



Example: Using _ Wildcard

Find employees whose names have 'n' as the second latter.

SELECT * FROM employee WHERE EmpName LIKE '_n%';

Output:

IDEmpNameCitySalary
2EnnaXYZ5000

Explanation: _n% matches names where the second letter is 'n'.



Key Point

  • Search names, emails, cities with partial matches
  • Case-insensitive in some databases like MySQL (by default)
  • Combine with NOT for inverse matches:
WHERE column_name NOT LIKE 'A%';