SQL LIKE Operator
SQL

SQL LIKE Operator — How to Search for Patterns in Text

CHAPTER 15 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL LIKE operator searches for a pattern in a text column. Unlike = which matches exact values, LIKE uses wildcards to find partial matches. In this chapter, you will learn how to use the % and _ wildcards to search strings in your database using the WHERE clause.

SQL LIKE Syntax

SQL — LIKE Syntax
SELECT column1, column2
FROM table_name
WHERE column LIKE 'pattern';

The Two LIKE Wildcards

Wildcard Meaning Example Matches
% Zero, one, or many characters 'M%' Mumbai, Marketing, Mike
_ Exactly one character '_ohn' John (not Johnathan)

You will explore these wildcards in much more detail in Chapter 16: SQL Wildcards. We will use this Employees table:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

% Wildcard Examples

Names starting with ‘A’:

SQL
SELECT Name FROM Employees
WHERE Name LIKE 'A%';
✓ OUTPUT
+-----------+
| Name      |
+-----------+
| Arun M.   |
+-----------+

Names containing ‘a’:

SQL
SELECT Name FROM Employees
WHERE Name LIKE '%a%';
✓ OUTPUT — Names containing ‘a’
+-----------+
| Name      |
+-----------+
| Priya K.  |
| Arun M.   |
| Emma W.   |
| Ravi S.   |
| Sara L.   |
+-----------+

Cities starting with ‘M’:

SQL
SELECT Name, City FROM Employees
WHERE City LIKE 'M%';
✓ OUTPUT
+-----------+--------+
| Name      | City   |
+-----------+--------+
| Priya K.  | Mumbai |
| Ravi S.   | Mumbai |
+-----------+--------+

Common LIKE Patterns

Pattern Meaning
'A%' Starts with A
'%a' Ends with a
'%an%' Contains “an” anywhere
'_o%' Second character is o
'J___' Starts with J, exactly 4 characters total
'%s' Ends with s

NOT LIKE

Use NOT LIKE to exclude rows matching a pattern. You can combine LIKE with AND, OR, and NOT operators for complex filters:

SQL
SELECT Name, Department FROM Employees
WHERE Department NOT LIKE 'S%';
✓ OUTPUT — Departments NOT starting with S
+-----------+------------+
| Name      | Department |
+-----------+------------+
| John D.   | Marketing  |
| Arun M.   | IT         |
| Ravi S.   | IT         |
| Sara L.   | HR         |
+-----------+------------+

⚠️ Case Sensitivity

LIKE is case-insensitive in MySQL (default) and SQL Server, but case-sensitive in PostgreSQL. In PostgreSQL, use ILIKE for case-insensitive pattern matching.

For a complete reference of pattern matching, see the official MySQL pattern matching documentation. In the next chapter, you will learn advanced SQL Wildcards including escaping and database-specific characters.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that finds all employees whose name starts with ‘S’ or whose city ends with ‘i’.

ANSWER
SELECT Name, City FROM Employees
WHERE Name LIKE 'S%'
   OR City LIKE '%i';

Expected Output:

+-----------+-----------+
| Name      | City      |
+-----------+-----------+
| Priya K.  | Mumbai    |
| Arun M.   | Delhi     |
| Ravi S.   | Mumbai    |
| Sara L.   | New York  |
+-----------+-----------+

📝 What You Learned in This Chapter

  • LIKE searches for patterns in text columns
  • % matches zero, one, or many characters
  • _ matches exactly one character
  • NOT LIKE excludes rows matching a pattern
  • LIKE is case-insensitive in MySQL, case-sensitive in PostgreSQL

Frequently Asked Questions

What does the % wildcard mean in SQL LIKE?
The % wildcard represents zero, one, or multiple characters. 'M%' matches any string starting with M (Mumbai, Marketing, Mike). '%a%' matches any string containing the letter a anywhere.
What is the difference between = and LIKE in SQL?
= matches an exact value. LIKE matches a pattern using wildcards. Use = when you know the exact value, use LIKE when you need partial or pattern-based matching.
Is SQL LIKE case sensitive?
It depends on the database. MySQL and SQL Server are case-insensitive by default. PostgreSQL is case-sensitive — use ILIKE for case-insensitive matching. Check your database settings to be sure.
Can I combine LIKE with AND and OR?
Yes. You can use LIKE with AND, OR, and NOT to create complex filters. For example: WHERE Name LIKE 'A%' OR Name LIKE 'S%' finds names starting with A or S.
Does LIKE work on numeric columns?
LIKE is designed for text columns. Some databases allow LIKE on numbers by implicitly converting them to text, but this is not recommended. Use comparison operators (=, >, <) for numeric filtering instead.

📖 Chapter 15 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com