The LIKE Operator in SQL – Practice Guide

Introduction

The LIKE operator is a powerful SQL feature that allows for pattern matching in string comparisons. Unlike the equality operator (=) which requires an exact match, LIKE enables flexible searches using wildcard characters.

LIKE Operator Syntax

Wildcard Characters

The LIKE operator uses two primary wildcard characters:

    • % (percent): Represents zero, one, or multiple characters
    • _ (underscore): Represents exactly one character

The ESCAPE Character

When you need to search for the actual % or _ characters in your data, use the ESCAPE clause:

Practice Exercises Using Oracle HR Schema

Exercise 1: Basic Pattern Matching

Let's start with simple pattern matching to find employees whose last names start with ‘K':

Explanation: This query returns all employees whose last name begins with ‘K' followed by any number of characters (including zero).

Exercise 2: Using the Underscore Wildcard

Find all employees whose first name is exactly 5 characters long:

Explanation: Each underscore represents exactly one character, so five underscores match any first name with exactly 5 characters.

Exercise 3: Combined Wildcards

Find employees whose last name has ‘a' as the second letter:

Explanation: This pattern matches any last name where:

    • The first character can be anything (_)
    • The second character must be ‘a'
    • The remaining characters can be anything or nothing (%)

Exercise 4: Finding Patterns Within Strings

Find all departments with ‘ing' anywhere in their name:

Explanation: The ‘%ing%' pattern searches for ‘ing' anywhere in the department name.

Exercise 5: Using the ESCAPE Clause

Find any job_id that contains a ‘%' character:

Explanation: This query searches for job_ids containing the % character. The backslash (\\) is defined as the escape character, telling SQL to treat the % after it as a literal character, not a wildcard.

Exercise 6: Case Sensitivity

Oracle's LIKE operator is case-sensitive by default. Find employees with first names that start with ‘a' (lowercase):

Now compare with employees whose names start with ‘A' (uppercase):

Explanation: These are different queries that will return different results due to case sensitivity.

Exercise 7: Case Insensitive Search

To perform a case-insensitive search, use the UPPER or LOWER functions:

Exercise 8: Finding Names with Specific Endings

Find employees whose last name ends with ‘son':

Exercise 9: Finding Job IDs with Underscore

Find all jobs with ‘REP' in their ID (note that job_ids contain underscores):

Explanation: This will find any job_id with ‘REP' anywhere in it.

Challenge Exercises

Challenge 1: Finding Specific Name Patterns

Find employees who have the same letter repeated twice in their last name:

Alternative solution using regular expressions (Oracle specific):

Challenge 2: Combined Conditions

Find employees who:

    • Have a job ID starting with ‘SA'
    • Have a last name containing ‘e'
    • Have a salary greater than 10000

Challenge 3: Find Managers in Department Names

Find all departments with ‘Manager' in their name, and list the employees who work in those departments:

Performance Considerations

Pattern matching with LIKE, especially with leading wildcards (‘%xyz'), can result in full table scans and poor performance. For large tables, consider:

    • Indexes: Standard B-tree indexes are ineffective for LIKE with leading wildcards
    • Function-based indexes: Can help with case-insensitive searches
    • Full-text indexing: For complex text searching beyond LIKE capabilities

LIKE vs. Regular Expressions

For more complex pattern matching, Oracle provides regular expression functions:

Summary

The LIKE operator is a versatile tool for pattern matching in SQL:

    • Use % for zero or more characters
    • Use _ for exactly one character
    • For literal % or _ characters, use the ESCAPE clause
    • Combine with other SQL conditions for powerful filtering
    • Consider case sensitivity in your searches
    • Be aware of performance implications with large datasets

Additional Exercises

To further practice the LIKE operator, try:

    • Find employees with three-part job IDs (two underscores)
    • Find locations with city names starting with a vowel
    • Find departments whose name contains the word “Sales” or “Marketing”
    • Find employees hired in a specific month across any year
    • Create a case-insensitive search for job titles containing “manager” or “director”
Rolar para cima