Part 3: Mastering the LIKE Command

Part 3: Mastering the LIKE Command | SQL Challenges & Capstone
SQL Filtering & Search Commands Practice · Part 3 · Lecture Notes

Mastering the LIKE Command

How to search text patterns inside column values β€” matching values that start with, end with, or contain specific text using the % wildcard.

Part 3 of 650% Complete
LIKE
Pattern Matching
Text Search
Business Search Logic
8 Practice Questions

Learning Objectives

  • Use LIKE in a WHERE clause to search for text patterns inside column values
  • Apply the % wildcard to match values that start with, end with, or contain a specific text
  • Search names, categories, statuses, and descriptions using partial text patterns
  • Understand that LIKE is case-insensitive in MySQL by default
Click any section header to open or close it

LIKE searches for patterns inside text values. Unlike WHERE with = (which requires an exact match), LIKE matches any value that fits a pattern β€” even if you only know part of the text you are looking for. It is the SQL equivalent of a search bar.

In industry, analysts use LIKE to search customer names, identify product records, filter business categories, locate partial matches across large datasets, and perform flexible text searches when exact values are unknown.

The Basic Structure

LIKE is always used inside a WHERE clause. You provide a pattern in single quotes, using % or _ as wildcards to represent the unknown parts of the text.

Basic LIKE Syntax
SELECT column_name FROM table_name WHERE column_name LIKE ‘pattern’;

LIKE allows analysts to search for text patterns rather than requiring a complete, exact value. The % symbol is the wildcard that makes this possible β€” it matches any sequence of characters, including no characters at all.

Three Core Patterns

LIKE ‘A%’ matches any value starting with A. LIKE ‘%son’ matches any value ending with son. LIKE ‘%SQL%’ matches any value containing SQL anywhere inside it. The position of % tells the database where the unknown part is.

Users Whose First Name Starts With A
SELECT * FROM users WHERE first_name LIKE ‘A%’;

LIKE works on any text column β€” names, descriptions, categories, statuses, campaign names. Wherever a column stores text, LIKE can search it. This is most valuable when looking for a word or phrase that might appear as part of a longer value.

Contains Search with %text%

Wrapping the search term in % on both sides β€” LIKE ‘%SQL%’ β€” returns any value that contains SQL anywhere. This is the most common LIKE pattern in business analytics.

Products Containing the Word SQL
SELECT * FROM products WHERE product_name LIKE ‘%SQL%’;
Campaigns Containing the Word Campaign
SELECT * FROM campaigns WHERE campaign_name LIKE ‘%Campaign%’;

In real analytical work, exact values are not always known. A product name might have changed, a status might be stored with different casing, or a campaign title might include extra words. LIKE handles all of these situations cleanly β€” returning anything that fits the pattern regardless of what surrounds it.

LIKE Is Case-Insensitive in MySQL

In MySQL, LIKE comparisons are case-insensitive by default on standard character sets. LIKE ‘canada’ and LIKE ‘Canada’ return the same results. This makes LIKE more forgiving than exact = comparisons for text searches.

Records Containing Canada in Country
SELECT * FROM users WHERE country LIKE ‘%Canada%’;

Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.

Practice 1 Retrieve users whose first_name starts with the letter S
AI Prompt
Write a MySQL query to show users whose first_name starts with the letter S.
SQL Answer
SELECT * FROM users WHERE first_name LIKE ‘S%’;
Practice 2 Retrieve products containing the word Data in the product_name
AI Prompt
Write a MySQL query to show products where product_name contains the word Data.
SQL Answer
SELECT * FROM products WHERE product_name LIKE ‘%Data%’;
Practice 3 Retrieve campaigns containing the word Campaign in the campaign_name
AI Prompt
Write a MySQL query to show campaigns where campaign_name contains the word Campaign.
SQL Answer
SELECT * FROM campaigns WHERE campaign_name LIKE ‘%Campaign%’;
Practice 4 Retrieve users whose country starts with United
AI Prompt
Write a MySQL query to show users whose country starts with United.
SQL Answer
SELECT * FROM users WHERE country LIKE ‘United%’;
Practice 5 Retrieve products whose product_name ends with Report
AI Prompt
Write a MySQL query to show products whose product_name ends with Report.
SQL Answer
SELECT * FROM products WHERE product_name LIKE ‘%Report’;
Practice 6 Retrieve support tickets where ticket_status contains the word Open
AI Prompt
Write a MySQL query to show support tickets where ticket_status contains the word Open.
SQL Answer
SELECT * FROM support_tickets WHERE ticket_status LIKE ‘%Open%’;
Practice 7 Retrieve subscriptions where status starts with A
AI Prompt
Write a MySQL query to show subscriptions where status starts with the letter A.
SQL Answer
SELECT * FROM subscriptions WHERE status LIKE ‘A%’;
Practice 8 Retrieve users whose last_name ends with son
AI Prompt
Write a MySQL query to show users whose last_name ends with son.
SQL Answer
SELECT * FROM users WHERE last_name LIKE ‘%son’;

Knowledge Check

Five questions β€” select the best answer for each.

1. What does LIKE do in a SQL WHERE clause?
AIt counts rows that match a condition
BIt sorts results alphabetically
CIt searches for text patterns inside column values
DIt removes duplicate values from the result
2. Which query returns users whose first_name starts with the letter L?
AWHERE first_name LIKE ‘%L’
BWHERE first_name LIKE ‘L%’
CWHERE first_name = ‘L%’
DWHERE first_name LIKE ‘_L’
3. Which LIKE pattern finds product names containing the word Dashboard?
ALIKE ‘Dashboard’
BLIKE ‘Dashboard%’
CLIKE ‘%Dashboard%’
DLIKE ‘%Dashboard’
4. What does LIKE ‘%son’ return?
AValues starting with son
BValues containing son anywhere
CValues ending with son
DValues with exactly 3 characters
5. In MySQL, is LIKE case-sensitive by default?
AYes β€” LIKE ‘canada’ and LIKE ‘Canada’ return different results
BNo β€” LIKE is case-insensitive on standard character sets
CIt depends on whether you use % or _
DOnly when used with exact matches

Part 3 Summary

The key ideas from this lecture β€” keep these in mind as you move forward.

LIKE allows analysts to search text patterns and perform flexible business searches when exact values are unknown. Use ‘A%’ for starts with, ‘%son’ for ends with, and ‘%word%’ for contains. LIKE is case-insensitive in MySQL by default.

LIKEPattern MatchingText SearchStarts WithEnds WithContainsCase-Insensitive%