SQL LIKE Operator
The SQL LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column. It helps in finding records that match a certain pattern.
Using LIKE Operator
The LIKE
operator is used in queries to match patterns in text columns. You can use wildcard characters to perform partial matches.
-- Find all employees with names starting with 'J'
SELECT * FROM Employees
WHERE EmployeeName LIKE 'J%';
-- Find all products with names containing 'Pro'
SELECT * FROM Products
WHERE ProductName LIKE '%Pro%';
Syntax
The basic syntax for using the LIKE
operator is:
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Wildcard Characters
The LIKE
operator uses wildcard characters to define the search pattern:
- %: Represents zero or more characters. Example:
'a%'
matches any string that starts with 'a'. - _: Represents a single character. Example:
'a_c'
matches 'abc', 'adc', etc.
Examples
Here are some examples of using the LIKE
operator:
-- Find all customers whose names end with 'son'
SELECT * FROM Customers
WHERE CustomerName LIKE '%son';
-- Find all orders with descriptions starting with 'Special'
SELECT * FROM Orders
WHERE OrderDescription LIKE 'Special%';