LIKE Operator

SQL · Filtering Rows

Theory

LIKE matches text against a pattern with two wildcards: % stands for any number of characters (including none) and _ stands for exactly one character. name LIKE 'A%' finds names starting with A, '%a' finds names ending in a, '%ee%' finds names containing ee, and '_a%' finds names whose second letter is a.

Case sensitivity depends on the engine and its collation: PostgreSQL LIKE is case-sensitive (it adds ILIKE for case-insensitive), while MySQL, SQL Server and SQLite are usually case-insensitive for plain letters. A pattern with a leading % cannot use a normal index, so it scans the whole table. To search for a literal % or _, use an ESCAPE character, e.g. LIKE '50\%%' ESCAPE '\'. NOT LIKE keeps rows that do not match.

Why we use it

Text data is messy and people often know only part of a value: a name prefix, a domain in an email, a code fragment. LIKE finds these partial matches without needing exact values.

When to use it

Use LIKE for simple prefix, suffix or contains searches. For exact values use = or IN. For complex patterns use regular expression functions, and for large-scale text search use full-text search features.

How it works

  1. Read a row
  2. Take the text value
  3. Walk the pattern: % = any run of chars, _ = exactly one char
  4. Does the whole value fit the pattern? Yes: Keep the row. No: Discard the row.
  5. Move to the next row

Syntax

SELECT columns FROM table
WHERE column LIKE 'pattern';

Example

SELECT * FROM employees WHERE name LIKE 'A%';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic