SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The LIKE
operator in SQL is used in a WHERE
clause to search for a specified pattern within a column. It's particularly useful when you're unsure about the exact value you're searching for. The two main wildcards used in conjunction with the LIKE
operator are:
Here are some examples to better understand the usage:
Finding any values that start with "a":
SELECT * FROM table_name WHERE column_name LIKE 'a%';
Finding any values that end with "a":
SELECT * FROM table_name WHERE column_name LIKE '%a';
Finding any values that have "or" in any position:
SELECT * FROM table_name WHERE column_name LIKE '%or%';
Finding any values that have "o" in the second position:
SELECT * FROM table_name WHERE column_name LIKE '_o%';
Finding any values that are three characters long and start with "an":
SELECT * FROM table_name WHERE column_name LIKE 'an_';
Finding any values that start with "a", have "o" in the third position, and end in "n":
SELECT * FROM table_name WHERE column_name LIKE 'a_o%n';
It's essential to remember that the LIKE
pattern matching is case-insensitive in some databases (like MySQL with the default collation) and case-sensitive in others (like PostgreSQL). Always be aware of how your specific database system treats this.
In scenarios where case-sensitivity matters and you're working with a database system where LIKE
is case-sensitive, you might need to convert both the column data and the match string to either uppercase or lowercase using functions like UPPER()
or LOWER()
:
SELECT * FROM table_name WHERE UPPER(column_name) LIKE UPPER('a%');
This ensures a case-insensitive match across systems where LIKE
is typically case-sensitive.
SQL LIKE operator examples:
SELECT * FROM employees WHERE last_name LIKE 'Sm%';
How to use LIKE in SQL:
SELECT * FROM products WHERE product_name LIKE 'A%';
Wildcard characters with LIKE in SQL:
%
(matches any sequence of characters) and _
(matches any single character) can be used with LIKE to create flexible patterns for matching.SELECT * FROM customers WHERE contact_name LIKE 'M%y';
Using LIKE for partial string matching:
SELECT * FROM products WHERE product_name LIKE '%apple%';
Case-sensitive and case-insensitive LIKE in SQL:
-- Case-insensitive SELECT * FROM users WHERE username LIKE 'john%'; -- Case-sensitive (example for SQL Server) SELECT * FROM users WHERE username COLLATE Latin1_General_CS_AS LIKE 'John%';
LIKE with multiple conditions in SQL:
SELECT * FROM orders WHERE customer_name LIKE 'A%' AND order_date LIKE '2023%';
Combining LIKE with other operators:
SELECT * FROM products WHERE product_name LIKE 'A%' AND unit_price > 10;
Using NOT LIKE in SQL:
SELECT * FROM employees WHERE job_title NOT LIKE '%Manager%';
Escape characters with LIKE in SQL:
SELECT * FROM products WHERE product_name LIKE '10!_%' ESCAPE '!';
LIKE and character classes in SQL:
SELECT * FROM customers WHERE phone_number LIKE '[2-9]%';
LIKE and numeric patterns in SQL:
SELECT * FROM orders WHERE order_number LIKE '2023%';
LIKE and full-text search in SQL Server:
SELECT * FROM documents WHERE CONTAINS(document_content, 'SQL Server');