LIKE

Overview

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. It's used for pattern matching with text data.

LIKE is essential for searching text when you don't know the exact value, allowing partial matches using wildcards (covered in detail in Wildcards tutorial).

Basic Syntax

LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;

Wildcards Used with LIKE

LIKE uses two main wildcards:

Wildcard Description Example
% Matches zero or more characters 'John%' matches 'John', 'Johnny', 'Johnson'
_ Matches exactly one character 'J_hn' matches 'John', 'Jean' but not 'Johnson'

Examples

Example 1: Starts With Pattern

Names starting with 'John'
SELECT * FROM Customers
WHERE name LIKE 'John%';

This matches: 'John', 'Johnny', 'Johnson', 'John Doe', etc.

Example 2: Ends With Pattern

Emails ending with '@example.com'
SELECT * FROM Customers
WHERE email LIKE '%@example.com';

Example 3: Contains Pattern

Names containing 'son'
SELECT * FROM Customers
WHERE name LIKE '%son%';

This matches: 'Johnson', 'Jackson', 'Anderson', 'Wilson', etc.

Example 4: Single Character Wildcard

Using underscore for single character
SELECT * FROM Customers
WHERE name LIKE 'J_hn';

This matches: 'John', 'Jean' (if name is 4 characters), but not 'Johnson' or 'Joan'.

Example 5: Fixed Length Pattern

Five-letter names starting with 'J'
SELECT * FROM Customers
WHERE name LIKE 'J____';

This matches: 'John', 'James', 'Joan' (if exactly 5 characters).

Example 6: Combining Patterns

Complex pattern
SELECT * FROM Products
WHERE product_name LIKE 'Laptop%Pro%';

This matches: 'LaptopPro', 'Laptop Professional', 'LaptopProMax', etc.

NOT LIKE

Use NOT LIKE to exclude rows that match a pattern:

Exclude pattern matches
SELECT * FROM Customers
WHERE email NOT LIKE '%@test.com';

LIKE with Multiple Conditions

Combine LIKE with AND/OR:

Multiple LIKE conditions
SELECT * FROM Customers
WHERE (name LIKE 'John%' OR name LIKE 'Jane%')
  AND email LIKE '%@example.com';

Case Sensitivity

LIKE behavior with case sensitivity varies by database:

Case-insensitive search (PostgreSQL)
-- PostgreSQL: Use ILIKE for case-insensitive
SELECT * FROM Customers
WHERE name ILIKE 'john%';

-- Other databases: Use UPPER or LOWER functions
SELECT * FROM Customers
WHERE UPPER(name) LIKE 'JOHN%';

Escaping Wildcards

To search for literal % or _, escape them:

Escape wildcards
-- Find values containing '%' (syntax varies by database)
SELECT * FROM Products
WHERE discount LIKE '%\%%' ESCAPE '\';

-- Find values containing '_'
SELECT * FROM Products
WHERE code LIKE '%\_%' ESCAPE '\';

-- Alternative: Use brackets (SQL Server)
SELECT * FROM Products
WHERE discount LIKE '%[%]%';

Performance Considerations

LIKE vs =

Use = for exact matches, LIKE for pattern matching:

Comparison
-- Exact match (faster, can use index)
SELECT * FROM Customers
WHERE name = 'John Doe';

-- Pattern match (slower, may not use index)
SELECT * FROM Customers
WHERE name LIKE 'John%';

Common Use Cases

Best Practices

Next Steps

Learn more about wildcards in the Wildcards tutorial, or explore other filtering options like IN.