Chapter 27: PostgreSQL LIKE Operator

The LIKE operator in PostgreSQL.

You’ve already learned SELECT, WHERE, AVG(), operators in general — now let’s master LIKE, because almost every real application needs to do “search-like” queries at some point (product search, user name lookup, email filtering, invoice number matching, etc.).

1. What does the LIKE operator actually do?

LIKE is a pattern-matching operator that compares a string value against a pattern containing wildcards.

It answers questions like:

  • Does this name start with “Rah”?
  • Does this product code end with “-IND”?
  • Does this email contain “@gmail.com”?
  • Does this description have the word “fantasy” anywhere?

PostgreSQL offers two versions of pattern matching:

Operator Case sensitivity Wildcards used Typical use case in India 2026 apps
LIKE Case-sensitive % and _ Exact business rules, codes, IDs
ILIKE Case-insensitive % and _ User-facing search (most common)

PostgreSQL also has regular expressions (~, ~*), but LIKE / ILIKE are simpler and faster for the majority of everyday searches.

2. The two magic wildcards (understand these deeply)

Wildcard Meaning Matches example Does NOT match example
% Zero or more of any characters ‘Rah%’ → Rahul, Rahim, Rachel ‘Rohit’ (doesn’t start with Rah)
_ Exactly one character (any) ‘A__a’ → Anna, Aria, Aisha ‘Aisha’ (has 5 letters), ‘Aa’

Important rule: LIKE matches the entire string — not just part of it (unless you put % at both ends).

3. Basic syntax patterns (what you’ll write 95% of the time)

SQL

4. Real, practical examples (using our students and books tables)

Assume these rows exist:

students table

id first_name last_name email roll_number
1 Aarav Patel aarav.patel@hyduni.edu HYD2023001
2 Priya Reddy priya.reddy22@gmail.com SEC2024005
3 Rahul Sharma rahul.sharma.2004@yahoo.com HYD2023012
4 Sneha Kumar sneha.kumar06@gmail.com HYD2023028
5 Meera Iyer meera.iyer@gmail.com HYD2024009

books table (partial)

isbn title author_last_name
978-0143414216 The God of Small Things Roy
978-9382563754 The Immortals of Meluha Tripathi
978-1408855683 Harry Potter and the Philosopher’s Stone Rowling

Example 1: Names starting with certain letters

SQL

→ Returns: Aarav, (maybe others starting with A/a)

Example 2: Ends with something

SQL

→ Returns: priya.reddy22@gmail.com, sneha.kumar06@gmail.com, meera.iyer@gmail.com

Example 3: Contains a word anywhere

SQL

→ Returns: Harry Potter and the Philosopher’s Stone

Example 4: Exact length or pattern with _

SQL

→ Matches: HYD2023001, HYD2023012, HYD2023028

Example 5: NOT LIKE (exclude patterns)

SQL

→ Returns: Priya Reddy (SEC…)

Example 6: Combining LIKE with other conditions

SQL

5. Common traps & best practices (real student & production lessons)

Mistake / Question Wrong / Surprising result Correct / Explanation
LIKE ‘rahul’ Matches only exactly “rahul” Use %rahul% if you want contains
LIKE ‘%rahul’ Matches only ends with rahul Use %rahul% for contains
LIKE ‘Rahul’ on column with ‘rahul’ No match (case-sensitive) Use ILIKE for user-facing search
LIKE ‘%’ Matches everything (including NULL) Careful — usually you want IS NOT NULL too
Performance on large table Slow if no index Add trigram index (pg_trgm) for %term% searches
Escaping % or _ in pattern % or _ treated as wildcard Use ESCAPE or escape manually: LIKE ‘%10\% off’

Best practice in 2026 Indian apps:

SQL

(Prepared statement / parameter)

When LIKE is too slow for contains-search → switch to pg_trgm extension + <% operator or full-text search.

6. Your mini practice right now (try in your database)

  1. Find all students whose email ends with @gmail.com (use ILIKE)
  2. Find books whose title contains “the” (case-insensitive)
  3. Find roll numbers that have “2023” anywhere in them
  4. Find students whose first name is exactly 5 letters long (use _)

Next topic?

Tell me:

  • Want deep dive into regular expressions (~ / ~*) vs LIKE?
  • pg_trgm extension for fast fuzzy / contains search?
  • Full-text search (@@ operator) for natural language search?
  • Or move to another aggregate / string function (CONCAT, SUBSTRING, etc.)?

Your teacher is ready — just say the word! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *