Lab 09
SQL Wildcards and Comparison Operations (LIKE, NOT, Pattern Matching)
Section 1: Concept Foundation
What are SQL Wildcards and the LIKE Operator?
SQL wildcards are special characters used in conjunction with the LIKE operator to perform pattern matching on string data. Instead of checking for exact equality, LIKE allows you to search for strings that match a specific pattern, such as "starts with S", "contains 'nes'", or "ends with 'h'".
Why was this feature created?
- Flexible Searching: Real-world data often requires fuzzy matching (e.g., searching for all customers whose name starts with "A").
- Pattern Discovery: In data analysis, you often need to find rows based on partial information.
- User-Friendly Queries: Applications with search bars use
LIKEto implement "contains" or "begins with" searches.
Real-world Analogy
Think of a library catalog search:
%is like a wildcard that can match any number of letters (e.g., "comput%" finds "computer", "computation", "computing")._is like a placeholder for exactly one letter (e.g., "c_t" finds "cat", "cot", "cut" but not "cart").
Where is it used?
- Search Filters: E-commerce sites (search for products containing "desk").
- Data Cleaning: Finding records with inconsistent formatting (e.g., phone numbers with or without dashes).
- Reporting: Filtering by name patterns, email domains, etc.
Section 2: Visual Understanding
LIKE Pattern Matching Flow
NOT Operator Negation Flow
Section 3: Syntax Breakdown and Oracle-Specific Rules
1. The LIKE Operator
General Syntax:
SELECT column_list
FROM table_name
WHERE column_name LIKE pattern;Explanation:
column_name: The character column to test.pattern: A string containing wildcards%and_.%: Matches any sequence of zero or more characters._: Matches exactly one character.
Important: In Oracle, LIKE is case-sensitive by default. Use UPPER() or LOWER() for case-insensitive matching.
Examples:
WHERE City LIKE 'S%'→ Cities starting with 'S'.WHERE City LIKE '%nes%'→ Cities containing 'nes' anywhere.WHERE City LIKE '_a%'→ Cities with 'a' as the second character.WHERE City LIKE '____'→ Cities with exactly 4 characters.
2. The NOT Operator
Purpose: Negates a condition. Returns rows where the condition is FALSE.
General Syntax:
SELECT column_list
FROM table_name
WHERE NOT condition;Explanation:
NOTcan be applied to any condition: comparison,LIKE,IN,BETWEEN,IS NULL, etc.NOTreverses the truth value.
Examples:
WHERE NOT City = 'Sandnes'→ Cities other than Sandnes.WHERE LastName NOT LIKE 'S%'→ Names not starting with 'S'.WHERE City NOT IN ('Sandnes', 'Stavanger')→ Cities not in the list.WHERE P_Id NOT BETWEEN 1 AND 2→ IDs outside the range.WHERE Address IS NOT NULL→ Rows with non‑null addresses.
3. Combining LIKE and NOT
SELECT * FROM Customers WHERE LastName NOT LIKE 'S%';4. Important Oracle Specifics
LIKEis case-sensitive in Oracle. If you want to ignore case, use:WHERE UPPER(LastName) LIKE 'S%'- Escape characters: If you need to search for actual
%or_, use theESCAPEclause (e.g.,WHERE column LIKE '%\_%' ESCAPE '\'). NOT LIKEis equivalent toNOT (column LIKE pattern).
Section 4: Query Thinking Process (Professional Approach)
Requirement: "Find all employees whose name ends with the letter 'h'."
Step 1: Identify table → employees
Step 2: Identify column → employee_name
Step 3: Determine pattern → Ends with 'h' → '%h'
Step 4: Choose operator → LIKE
Step 5: Write query:
SELECT * FROM employees WHERE employee_name LIKE '%h';Step 6: Validate → Check that results only include names ending with 'h'.
Section 5: Multiple Examples
Example 1: Easy (Starts with)
Requirement: Find cities starting with "sa" from Persons table.
Query:
SELECT * FROM Persons WHERE City LIKE 'sa%';Example 2: Medium (Contains pattern)
Requirement: Find last names containing "sen". Query:
SELECT * FROM Persons WHERE LastName LIKE '%sen%';Example 3: Exam-style (Exact number of characters)
Requirement: Find first names that are exactly 4 characters long. Query:
SELECT * FROM Persons WHERE FirstName LIKE '____';Example 4: Real-world (Email domain search)
Requirement: Find customers whose email address ends with "@gmail.com". Query:
SELECT * FROM Customers WHERE Email LIKE '%@gmail.com';Example 5: Tricky (NOT LIKE with multiple patterns)
Requirement: Find products whose description does not contain the word "Desk". Query:
SELECT * FROM Product_T WHERE ProductDescription NOT LIKE '%Desk%';Example 6: Combining NOT with IN and LIKE
Requirement: Find customers whose city is neither 'Sandnes' nor starts with 'S'. Query:
SELECT * FROM Customers WHERE City NOT IN ('Sandnes') AND City NOT LIKE 'S%';Section 6: Pattern Recognition
| Requirement Keyword | Think of | SQL Feature |
|---|---|---|
| "Starts with" | LIKE 'pattern%' | WHERE column LIKE 'A%' |
| "Ends with" | LIKE '%pattern' | WHERE column LIKE '%A' |
| "Contains" | LIKE '%pattern%' | WHERE column LIKE '%A%' |
| "Second character is" | LIKE '_pattern' | WHERE column LIKE '_A%' |
| "Exactly n characters" | LIKE '____' (n underscores) | WHERE column LIKE '____' |
| "Does not start with" | NOT LIKE 'pattern%' | WHERE column NOT LIKE 'A%' |
| "Does not contain" | NOT LIKE '%pattern%' | WHERE column NOT LIKE '%A%' |
| "Not equal to" | NOT condition | WHERE NOT column = 'value' |
| "Not in list" | NOT IN | WHERE column NOT IN (v1, v2) |
Section 7: Common Mistakes
| Mistake | Wrong Query | Why Wrong | Correct Query |
|---|---|---|---|
Using = instead of LIKE | WHERE City = 'S%' | = does not interpret wildcards; it searches for the literal string "S%". | WHERE City LIKE 'S%' |
Forgetting the % at the end for "starts with" | WHERE City LIKE 'S' | Matches only 'S', not 'Sandnes'. | WHERE City LIKE 'S%' |
Using % for exactly one character | WHERE LastName LIKE '%son' | This will match any name ending with 'son', not just 3-letter prefixes. | Use _ for exact single characters. |
| Case sensitivity issues | WHERE LastName LIKE 's%' | Will not match 'Smith' if column has 'Smith' (capital S). | Use UPPER(LastName) LIKE 'S%' |
Using NOT with NULL incorrectly | WHERE NOT Age = NULL | NULL comparisons are always false; this returns no rows. | WHERE Age IS NOT NULL |
Mixing AND/OR precedence without parentheses | WHERE product LIKE '%Desk' OR product LIKE '%Table' AND price > 300 | AND has higher precedence than OR; this returns all 'Desk' products, and only 'Table' products with price > 300. | WHERE (product LIKE '%Desk' OR product LIKE '%Table') AND price > 300 |
Section 8: Exam Perspective
Frequently Asked Questions
- Write a query to find all employees whose names start with 'J'.
- Write a query to find customers whose email contains 'gmail'.
- What is the difference between
%and_in theLIKEoperator? - How do you find products whose description does not contain the word 'Desk'?
- Write a query to find all cities that are exactly 5 characters long.
Viva Questions
- Is
LIKEcase-sensitive in Oracle? (Yes, by default.) - How would you perform a case‑insensitive search using
LIKE? (UseUPPER()orLOWER()on both sides.) - Can you use
LIKEon numeric columns? (No,LIKEworks only on character data. UseTO_CHARif needed, but avoid.) - What does
NOT LIKEdo? (It returns rows that do not match the pattern.) - How do you escape wildcards if you need to search for actual
%or_? (Use theESCAPEclause.)
MCQ
-
Which wildcard matches exactly one character? a) % b) * c) _ d) ? Answer: c
-
To find names ending with 'son', you use: a)
LIKE '%son'b)LIKE 'son%'c)LIKE '%son%'d)LIKE '_son'Answer: a -
Which operator negates a condition? a) AND b) OR c) NOT d) XOR Answer: c
Section 9: Industry Perspective
How professionals use LIKE and NOT
- Search Boxes: Web applications use
LIKEto implement autocomplete and search suggestions. - Data Validation:
NOT LIKEto detect invalid formats (e.g., phone numbers that contain letters). - ETL Pipelines: Filtering out records with patterns indicating test data (e.g.,
LIKE 'test%'). - Performance:
LIKEwith leading%('%pattern') prevents index usage and can be slow. Professionals often use full‑text search (Oracle Text) for heavy text searching.
Best Practices
- Avoid leading
%if possible: For performance, prefer patterns that start with a fixed string (e.g.,'A%') to leverage indexes. - Use
UPPERfor case‑insensitive searches but be aware that function‑based indexes can help. - Escape special characters when user input contains
%or_. - Combine
NOT LIKEcarefully with other conditions to ensure logical precedence.
Section 10: Query Construction Framework
For any pattern matching task:
- Identify the column (must be character type).
- Determine the pattern (starts, ends, contains, exact length).
- Choose wildcards:
%for any number of characters._for exactly one character.
- Write the
LIKEpattern. - Add
NOTif exclusion is needed. - Consider case sensitivity and apply
UPPER()if needed. - Wrap in parentheses when combining with other conditions.
- Write the final query and test.
Section 11: Practice Section
Beginner Exercises (Use Product_T table)
- Find all products whose
ProductDescriptionstarts with 'C'. - Find products whose
ProductFinishends with 'h'. - Find products whose
ProductStandardPriceis 375, but description contains 'Desk'. - Find all products where
ProductFinishis exactly 'Cherry'. - Find products where
ProductIDdoes not start with '1'.
Intermediate Exercises
- Find products whose
ProductDescriptioncontains the word 'Table'. - Find products whose
ProductFinishis 'Natural Ash' and description does not contain 'Desk'. - Find products with
ProductStandardPrice> 400 andProductDescriptionlike '%Desk%'. - Find products with
ProductFinishnot like '%Ash%'. - Find all products whose
ProductDescriptionhas exactly 10 characters (hint: useLENGTHandLIKE? Better useLENGTH(description)=10; however,LIKEcan also do exact length with underscores:'__________').
Advanced Exercises
- Write a query to find customers whose name starts with 'A' and ends with 's'.
- Find all employees whose
employee_namecontains the letter 'a' twice (useLIKE '%a%a%'). - Write a query that returns products where the
ProductDescriptiondoes not contain any of the words 'Desk', 'Table', 'Center' (useNOT LIKEwith multipleANDconditions). - Find products where
ProductFinishis 'Cherry' or 'Walnut' and the price is between 200 and 400, but description does not contain 'Desk'. - Using the
employeestable from the lab, find all employees whose name starts with 'J' and salary > 50000.
Section 12: Challenge Section – Real-world Scenarios
Scenario 1: E-commerce Search
- A user searches for "chair". Write a query to find all products whose
ProductDescriptioncontains the word "chair" (case‑insensitive).
Scenario 2: HR Filter
- HR wants to send emails to all employees whose email address is not from the company domain (i.e., does not end with '@company.com'). Write a query.
Scenario 3: Inventory Audit
- The warehouse manager wants to identify products that have a
ProductFinishthat is not one of the standard finishes: 'Cherry', 'Walnut', 'Natural Ash'. UseNOT INwith a list.
Section 13: Interview Preparation
Conceptual Questions
-
Explain the difference between
%and_in SQL pattern matching.%matches any sequence of zero or more characters;_matches exactly one character.
-
How do you perform a case‑insensitive search using
LIKEin Oracle?- Use
UPPER(column) LIKE UPPER(pattern)orLOWER(column) LIKE LOWER(pattern).
- Use
-
What is the performance impact of using
LIKE '%pattern%'?- It prevents the use of regular indexes because the leading
%means the search cannot start from the beginning of the string. For large tables, this can be slow; consider using Oracle Text or a full‑text index.
- It prevents the use of regular indexes because the leading
-
Can you use
LIKEon numbers?- No,
LIKEexpects character data. You can convert numbers to strings usingTO_CHAR, but it's not recommended for performance.
- No,
-
How do you escape a wildcard character if you want to search for a literal
%?- Use the
ESCAPEclause:WHERE column LIKE '%\%%' ESCAPE '\'finds strings containing%.
- Use the
Practical SQL Interview Questions
Q1: Write a query to find all customers whose last name starts with 'M' and ends with 'n'. Answer:
SELECT * FROM Customers WHERE LastName LIKE 'M%n';Q2: Write a query to find products whose description contains the word 'Desk' but not 'Computer'. Answer:
SELECT * FROM Product_T
WHERE ProductDescription LIKE '%Desk%'
AND ProductDescription NOT LIKE '%Computer%';Q3: How would you find all employees whose name does NOT contain the letter 'e'? Answer:
SELECT * FROM Employees WHERE EmployeeName NOT LIKE '%e%';Q4: Write a query to find all products with price > 300, but only those whose finish is either 'Cherry' or 'Walnut' and the description ends with 'Desk'. Answer:
SELECT * FROM Product_T
WHERE ProductStandardPrice > 300
AND ProductFinish IN ('Cherry', 'Walnut')
AND ProductDescription LIKE '%Desk';Q5: What is the difference between the following two queries?
-- Query A
SELECT * FROM Products WHERE Description LIKE '%Table' OR Description LIKE '%Desk' AND Price > 300;
-- Query B
SELECT * FROM Products WHERE (Description LIKE '%Table' OR Description LIKE '%Desk') AND Price > 300;Answer: In Query A, due to operator precedence (AND before OR), it returns all products whose description ends with 'Table' (regardless of price) plus those that end with 'Desk' and have price > 300. Query B returns only products that end with either 'Table' or 'Desk' AND have price > 300. This is a classic trap in exam questions.
Section 14: Topic Summary – Cheat Sheet
LIKE Patterns Quick Reference
| Pattern | Meaning | Example |
|---|---|---|
'A%' | Starts with 'A' | WHERE City LIKE 'L%' |
'%A' | Ends with 'A' | WHERE City LIKE '%n' |
'%A%' | Contains 'A' | WHERE City LIKE '%e%' |
'_A%' | Second character is 'A' | WHERE City LIKE '_a%' |
'____' | Exactly 4 characters | WHERE City LIKE '____' |
'A_B' | 3 characters: A, any, B | WHERE Name LIKE 'A_B' |
NOT Operator Usage
| With | Example | Effect |
|---|---|---|
= | WHERE NOT City = 'Sandnes' | Excludes Sandnes |
LIKE | WHERE LastName NOT LIKE 'S%' | Excludes names starting with S |
IN | WHERE City NOT IN ('Sandnes','Stavanger') | Excludes those cities |
BETWEEN | WHERE P_Id NOT BETWEEN 1 AND 2 | Excludes IDs 1 and 2 |
IS NULL | WHERE Address IS NOT NULL | Includes rows with non‑null addresses |
Key Takeaways
LIKEis for pattern matching;%and_are the wildcards.NOTreverses any condition.- Be mindful of case sensitivity and operator precedence when combining with
AND/OR. - Use parentheses to clarify logic.
Final Professional Advice
The LIKE operator is a powerful tool for textual data analysis, but it must be used with awareness of its performance implications. In production, avoid leading wildcards if possible. Always test with sample data to ensure your patterns match as intended. Combine NOT and LIKE wisely to filter out unwanted patterns. With practice, you'll be able to write precise, efficient, and readable queries for any pattern‑based requirement.