🔍

SQL Pattern Matching

Jun 30, 2025

Overview

This lecture explains how to use the SQL LIKE and NOT LIKE keywords in the WHERE clause to search for text patterns in database queries.

Using WHERE with Specific Values

  • Using WHERE ... IN (...) selects rows where the column matches any value in the list.
  • Listing every possible value is not practical for large datasets.

Introducing LIKE for Pattern Matching

  • LIKE allows searching for text patterns instead of specifying every possible value.
  • To find names starting with "l", use WHERE column LIKE 'l*'.
  • The asterisk (*) is used as a wildcard to represent any number of characters.

Pattern Matching Variations

  • 'l*' finds names starting with "l".
  • '*l*' finds names containing "l" anywhere.
  • '*l' finds names ending with "l".
  • Wildcards can be combined for flexible searches.

Combining LIKE Statements

  • Multiple LIKE conditions can be combined with OR, but overlapping patterns may cause duplicate results.
  • Using '*l*' alone covers names with "l" at any position, making additional conditions redundant.

Using NOT LIKE

  • NOT LIKE returns rows where the column does not match the specified pattern.
  • Example: WHERE column NOT LIKE '*l*' finds names without the letter "l".

Key Terms & Definitions

  • WHERE — SQL clause for filtering results based on conditions.
  • IN — SQL operator for matching any value in a specified list.
  • LIKE — SQL operator for pattern matching with wildcards.
  • Wildcard (*) — Placeholder character representing any string of characters in pattern searches.
  • NOT LIKE — SQL operator for filtering out rows that match a pattern.

Action Items / Next Steps

  • Practice writing SQL queries using LIKE and NOT LIKE with different wildcard placements.
  • Prepare for the next lecture on filtering data using date ranges.