SQL LIKE Get Certified
Learn how to search for text patterns in database columns using the SQLite-compatible LIKE operator.
What is the LIKE Operator?
The LIKE operator is used with the WHERE clause to search for a pattern in a column.
It is useful when you do not know the exact value you want to find.
SQLite supports two wildcard characters with LIKE: % (zero or more characters) and _ (exactly one character).
LIKE Wildcards
%'A%'_'_a%'Basic LIKE Syntax
The column name goes before LIKE and the search pattern goes after it, inside quotes.
What this code does: Selects every column of the rows whose name starts with the letter A. The % after the A means “any characters may follow”. The query returns 1 row: Ava Thomas (Marketing, 59000).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT *
FROM employees
WHERE name LIKE 'A%';
Starts With a Pattern
To find names that start with a letter or text, put the % wildcard after it.
What this code does: Returns the names that begin with M. The query returns 2 rows: Michael Brown and Matthew Anderson.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name
FROM employees
WHERE name LIKE 'M%';
Ends With a Pattern
To find values that end with a letter or text, put the % wildcard before it.
What this code does: Returns the names whose last character is n. The query returns 4 rows: Emily Johnson, Michael Brown, Sophia Wilson and Matthew Anderson.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name
FROM employees
WHERE name LIKE '%n';
Contains a Pattern
Put % on both sides of the text to find values that contain it anywhere.
What this code does: Returns the names that contain the letters “an” anywhere, at the start, middle or end. Because SQLite’s LIKE ignores case for English letters, “An” in Anderson also matches. The query returns 2 rows: Daniel Moore and Matthew Anderson.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name
FROM employees
WHERE name LIKE '%an%';
The _ Wildcard
The underscore (_) wildcard stands for exactly one character, no more and no less.
What this code does: In '_a%' the underscore accepts any single first character, then the second character must be a, and % accepts the rest. It finds names whose second letter is a. The query returns 3 rows: James Smith, Daniel Moore and Matthew Anderson.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name
FROM employees
WHERE name LIKE '_a%';
Using Multiple _ Wildcards
Use several underscores to match a fixed number of unknown characters.
What this code does: The pattern 'S____' is the letter S followed by exactly four characters, so it matches only five-letter departments that start with S. Only Sales fits; Marketing, Finance and Engineering are longer, and HR is shorter. The query returns 2 rows: Michael Brown (Sales) and Daniel Moore (Sales).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, department
FROM employees
WHERE department LIKE 'S____';
LIKE with Numbers
LIKE works on the text form of numbers, so you can search for IDs that begin with certain digits.
What this code does: This table has an employee_id column. SQLite converts the numbers to text before matching, so '10%' finds every ID that starts with 10. The query returns 5 rows: 101 (James Smith), 102 (Emily Johnson), 103 (Michael Brown), 104 (Olivia Davis) and 105 (William Miller).
WITH employees(employee_id, name) AS (
VALUES
(101, 'James Smith'),
(102, 'Emily Johnson'),
(103, 'Michael Brown'),
(104, 'Olivia Davis'),
(105, 'William Miller'),
(206, 'Sophia Wilson'),
(207, 'Daniel Moore'),
(208, 'Emma Taylor'),
(209, 'Matthew Anderson'),
(210, 'Ava Thomas')
)
SELECT employee_id, name
FROM employees
WHERE employee_id LIKE '10%';
LIKE with WHERE
LIKE is used inside WHERE, and it can test any text column, not only names.
What this code does: Tests the department column instead of name and returns the employees in departments that start with S. The query returns 2 rows: Michael Brown (Sales) and Daniel Moore (Sales).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, department
FROM employees
WHERE department LIKE 'S%';
LIKE with AND
Combine LIKE with other conditions using AND. Both conditions must be true.
What this code does: Keeps only the employees whose name contains the letter a and whose salary is above 60000. A row that fails either test is removed. The query returns 6 rows: James Smith (95000), Olivia Davis (88000), William Miller (72000), Daniel Moore (66000), Emma Taylor (81000) and Matthew Anderson (102000).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
WHERE name LIKE '%a%'
AND salary > 60000;
LIKE with OR
Use OR to search for more than one pattern. A row needs to match only one of them.
What this code does: Returns the employees whose name starts with A or starts with D. The query returns 2 rows: Daniel Moore (Sales) and Ava Thomas (Marketing).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, department
FROM employees
WHERE name LIKE 'A%'
OR name LIKE 'D%';
NOT LIKE
NOT LIKE returns the rows that do not match the pattern.
What this code does: Returns the employees whose name does not contain the letter a. The query returns 1 row: Emily Johnson (Marketing).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, department
FROM employees
WHERE name NOT LIKE '%a%';
LIKE with ORDER BY
Combine LIKE with ORDER BY to sort the matching rows.
What this code does: Finds the names that contain the letter a and sorts them from the highest salary to the lowest. The query returns 9 rows: Matthew Anderson (102000), James Smith (95000), Olivia Davis (88000), Emma Taylor (81000), William Miller (72000), Daniel Moore (66000), Ava Thomas (59000), Michael Brown (58000) and Sophia Wilson (54000).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, salary
FROM employees
WHERE name LIKE '%a%'
ORDER BY salary DESC;
LIKE with LIMIT
SQLite lets you combine LIKE with LIMIT to return only a set number of matches.
What this code does: Filters the names that contain the letter a and then keeps at most five of them. With no ORDER BY, they are the first matches in table order. The query returns 5 rows: James Smith (Engineering), Michael Brown (Sales), Olivia Davis (Engineering), William Miller (Finance) and Sophia Wilson (HR).
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name, department
FROM employees
WHERE name LIKE '%a%'
LIMIT 5;
Case Sensitivity in SQLite
In SQLite, LIKE is case-insensitive for ASCII letters by default, so 'a%' matches names that start with either a or A.
What this code does: The pattern uses a lowercase a, but the names are stored with capital letters. SQLite still matches them. The query returns 1 row: Ava Thomas.
WITH employees(name, department, salary) AS (
VALUES
('James Smith', 'Engineering', 95000),
('Emily Johnson', 'Marketing', 62000),
('Michael Brown', 'Sales', 58000),
('Olivia Davis', 'Engineering', 88000),
('William Miller', 'Finance', 72000),
('Sophia Wilson', 'HR', 54000),
('Daniel Moore', 'Sales', 66000),
('Emma Taylor', 'Finance', 81000),
('Matthew Anderson', 'Engineering', 102000),
('Ava Thomas', 'Marketing', 59000)
)
SELECT name
FROM employees
WHERE name LIKE 'a%';
Common LIKE Patterns
'A%''%n''%an%''_a%''S____'Advantages of LIKE
- Makes it easy to search for text patterns.
- Useful when the exact value is unknown.
- Supports flexible searches using wildcards.
- Can be combined with
WHERE,AND,OR,ORDER BYandLIMIT. - Works directly with browser-based SQLite execution.
Best Practices
- Use
%to match zero or more characters. - Use
_to match exactly one character. - Remember that a pattern starting with
%cannot use an index and can be slow on large tables. - Use
ORDER BYwhen matching results need to be sorted. - Use
LIMITwhen you only need a few matching records.
LIKE is the SQL tool for pattern matching. Combine % and _ to build flexible text searches.
Click Test My Code on any example above to run it live, or
Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.