SQL ‘Like’ Command Cheat Sheet

The LIKE command in SQL is used to search for a pattern within a text/string column. It’s very useful when you don’t want an exact match but want to find strings that follow a certain pattern. Here’s a clear breakdown:


Basic Syntax





SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';
  • column_name → the column you want to search.
  • 'pattern' → the text pattern you are looking for.
  • Patterns are case-insensitive in some DBs (like MySQL with default collation) or case-sensitive in others (like PostgreSQL).

Wildcards

LIKE uses wildcards to define flexible patterns:

WildcardMeaningExample
%Any number of characters (including zero)'a%' → anything starting with a
_Exactly one character'a_'ab, ac but not abc

Examples

  1. Find names starting with ‘J’




SELECT name
FROM users
WHERE name LIKE 'J%';

Matches: John, Jane, Jack
Does not match: Bob, alice


  1. Find names ending with ‘n’




SELECT name
FROM users
WHERE name LIKE '%n';

Matches: John, Ethan
Does not match: Jane, Bob


  1. Find names with exactly 4 letters




SELECT name
FROM users
WHERE name LIKE '____';

Matches: John, Mary
Does not match: Ethan (5 letters)


  1. Find names containing ‘ar’ anywhere




SELECT name
FROM users
WHERE name LIKE '%ar%';

Matches: Mary, Carol
Does not match: John, Bob


Posted

in

by

Tags:

Comments

Leave a Reply

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