Checking your session…
Module 08: Data Filtering and Operators

8.7 LIKE / NOT LIKE and Wildcards

Level: 8 | Version: v1.1 | Author: Meptrasoft

Overview

LIKE is used to match text against a pattern.

NOT LIKE is used to exclude text that matches a pattern.

Operator Purpose
LIKE Matches a pattern
NOT LIKE Excludes a pattern

Two main wildcards are used:

Wildcard Meaning
% Any number of characters, including zero
_ Exactly one character

LIKE → Match a pattern
NOT LIKE → Exclude a pattern

SQL LIKE, NOT LIKE & Wildcards: Matching and excluding text patterns
Figure 1: LIKE matches text patterns, NOT LIKE excludes matching patterns, and % and _ define how text is matched.

Practice Table

This topic reuses the employees table from 8.1 The WHERE Clause.

emp_id emp_name dept_id salary
1 John Smith 10 45000
2 Emma Brown 20 60000
3 Samuel Reed 10 32000
4 Anderson 30 55000
5 Sarah Wilson 20 48000
6 Tom Watson 10 51000

LIKE

LIKE matches text using a pattern.

Starts With

SQL
SELECT *
FROM employees
WHERE emp_name LIKE 'S%';

S% means:

Starts with S, followed by any number of characters.

This matches:

TEXT
Samuel Reed
Sarah Wilson

Ends With

SQL
SELECT *
FROM employees
WHERE emp_name LIKE '%son';

%son means:

Ends with son.

For example:

TEXT
Anderson

Contains

SQL
SELECT *
FROM employees
WHERE emp_name LIKE '%am%';

%am% means the text am can appear anywhere in the value.

LIKE → Find text that matches a pattern


NOT LIKE

NOT LIKE excludes values that match the pattern.

SQL
SELECT *
FROM employees
WHERE emp_name NOT LIKE '%son';

This returns names that do not end with son.

NOT LIKE → Exclude matching patterns


Wildcard %

% matches zero or more characters.

Examples:

TEXT
'a%'    → Starts with a
'%a'    → Ends with a
'%a%'   → Contains a

For example:

SQL
SELECT *
FROM employees
WHERE emp_name LIKE 'J%';

matches names beginning with J.


Wildcard _

_ matches exactly one character.

Example:

SQL
SELECT *
FROM employees
WHERE emp_name LIKE '_a%';

Here:

TEXT
_ → first character
a → second character
% → remaining characters

So the second character must be a.


Common Patterns

Pattern Meaning
'S%' Starts with S
'%son' Ends with son
'%am%' Contains am
'S_' Starts with S and has exactly 2 characters
'_a%' Second character is a
'__a%' Third character is a

Easy Memory Trick

TEXT
% → MANY / ANY CHARACTERS
_ → ONE CHARACTER

LIKE vs NOT LIKE

TEXT
LIKE
→ MATCH THE PATTERN

NOT LIKE
→ EXCLUDE THE PATTERN

Common Beginner Mistakes

✗ Mistake ✓ Correct Understanding
% means exactly one character % means zero or more characters
_ means any number of characters _ means exactly one character
LIKE 'S%' finds names containing S anywhere It finds values starting with S
LIKE '%son' finds values containing son anywhere It finds values ending with son
NOT LIKE removes data It only filters the query result
= and LIKE always behave the same LIKE is used for pattern matching

Placement Quick Points

TEXT
LIKE
→ MATCH PATTERN

NOT LIKE
→ EXCLUDE PATTERN

%
→ ZERO OR MORE CHARACTERS

_
→ EXACTLY ONE CHARACTER
  • LIKE is used for pattern matching.
  • NOT LIKE excludes matching patterns.
  • % matches zero or more characters.
  • _ matches exactly one character.
  • LIKE 'S%' finds values starting with S.
  • LIKE '%son' finds values ending with son.
  • LIKE '%am%' finds values containing am.

Interview Questions

What is LIKE in SQL?

LIKE is used to match text against a specified pattern.

What is NOT LIKE?

NOT LIKE returns rows whose values do not match the specified pattern.


Practice & Hands-On Exercises

Using the employees table:

  1. Find employees whose name starts with S:
SQL
SELECT * FROM employees WHERE emp_name LIKE 'S%';
  1. Find employees whose name ends with son:
SQL
SELECT * FROM employees WHERE emp_name LIKE '%son';
  1. Find employees whose name contains am:
SQL
SELECT * FROM employees WHERE emp_name LIKE '%am%';
  1. Find employees whose name does not end with son:
SQL
SELECT * FROM employees WHERE emp_name NOT LIKE '%son';
  1. Find names where the second character is a:
SQL
SELECT * FROM employees WHERE emp_name LIKE '_a%';
  1. Explain the difference between % and _.
  2. Explain the difference between LIKE and NOT LIKE.

💡 Tip: Test your queries using the in-browser interactive runner above.


Key Takeaway

TEXT
LIKE    → MATCH
NOT LIKE → EXCLUDE

% → ANY NUMBER OF CHARACTERS
_ → EXACTLY ONE CHARACTER

LIKE and NOT LIKE are used for text pattern matching, while % and _ define how the pattern should match.

End of LIKE / NOT LIKE and Wildcards