๐Ÿงฌ SQL Operators & Conditions
Estimated reading: 3 minutes 59 views

๐Ÿงฉ SQL LIKE โ€“ Pattern Matching with Wildcards in SQL

๐Ÿงฒ Introduction โ€“ Why Use SQL LIKE?

The LIKE operator in SQL is used to search for specific patterns in string values. It’s a powerful tool for performing partial matches, flexible filtering, and simple text analysis.

๐ŸŽฏ In this guide, you’ll learn:

  • How to use LIKE with % and _ wildcards
  • Combine LIKE with WHERE and logical conditions
  • Handle case-sensitivity across different databases
  • Real-world examples of pattern matching

โœ… 1. Basic LIKE Syntax

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

โœ… Returns names starting with “A”.


๐Ÿ”ง 2. Pattern Matching Wildcards

SymbolMeaningExampleMatches
%Zero or more characters'J%'John, Jake, Jasmine
_Exactly one character'J_n'Jon, Jan, Jim

๐Ÿงช 3. Examples of SQL LIKE Patterns

-- Names that start with A
SELECT * FROM employees WHERE name LIKE 'A%';

-- Emails ending with '.com'
SELECT * FROM contacts WHERE email LIKE '%.com';

-- Cities with 'new' in the middle
SELECT * FROM locations WHERE city LIKE '%new%';

-- Postal codes that are exactly 5 digits
SELECT * FROM addresses WHERE postal_code LIKE '_____'  -- five underscores

๐Ÿ” 4. Using NOT LIKE

SELECT * FROM products WHERE name NOT LIKE '%apple%';

โœ… Returns all products that do not contain “apple”.


โš ๏ธ 5. Case Sensitivity in LIKE

  • MySQL: Case-insensitive by default (unless BINARY keyword used)
  • PostgreSQL: Case-sensitive by default
  • SQL Server: Depends on the collation

๐Ÿ’ก Use ILIKE in PostgreSQL for case-insensitive matching.


๐Ÿง  6. LIKE with Logical Operators

SELECT * FROM users
WHERE name LIKE 'A%' OR name LIKE 'B%';

โœ… Combines multiple LIKE patterns using OR.


๐Ÿ“˜ Best Practices

โœ… Do ThisโŒ Avoid This
Use % and _ preciselyOverusing % at both ends for large tables
Use ILIKE in PostgreSQL if neededAssuming all LIKE queries are case-insensitive
Optimize indexed columnsIgnoring performance impact of wildcards

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

SQL LIKE is your go-to tool for performing text pattern matching. With % and _, you can construct expressive search filters across a wide variety of string data.

๐Ÿ” Key Takeaways:

  • % matches any number of characters; _ matches exactly one
  • Combine LIKE with WHERE, NOT, and OR for flexible searches
  • Behavior varies based on RDBMS case sensitivity settings

โš™๏ธ Real-World Relevance:
Used in search bars, product filters, customer lookup, and data cleansing operations.


โ“ FAQ โ€“ SQL LIKE

โ“ What is the difference between LIKE and =?

โœ… LIKE supports pattern matching; = checks for an exact match.

โ“ Does LIKE support case-insensitive search?

โœ… Depends on your database engine. Use ILIKE for PostgreSQL.

โ“ Can I use LIKE with numbers?

โœ… Yes, but numbers are implicitly cast as strings.

โ“ Can I match only one character?

โœ… Use _ (underscore) to match exactly one character.

โ“ What is the performance impact of LIKE?

โœ… It can be slow with leading % wildcards unless indexed or optimized.


Share Now :

Leave a Reply

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

Share

๐Ÿงฉ SQL LIKE

Or Copy Link

CONTENTS
Scroll to Top