LIKE searches text values for a specified pattern, while the % wildcard represents zero, one, or many characters. It is normally used in a WHERE clause to select records whose text matches that pattern.
The general syntax is:
SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';
For example, suppose a Student table contains the surnames Smith, Singh, Simpson, and Jones:
SELECT surname
FROM Student
WHERE surname LIKE 'Si%';
The pattern 'Si%' means the value must begin with Si, followed by any sequence of characters. This query returns Singh and Simpson. Because % can also represent zero characters, it would match a surname containing only Si.
The position of % changes the matching condition:
| Pattern | Meaning | Example matches |
|---|---|---|
'A%' | Begins with A | Ali, Anderson, A |
'%son' | Ends with son | Wilson, Anderson |
'%ann%' | Contains ann anywhere | Hannah, Joanna |
'J%n' | Begins with J and ends with n | John, Jackson |
The pattern must usually be enclosed in quotation marks because it is a string value. Whether matching is case-sensitive depends on the database management system and its configured collation, so 'a%' may or may not match Ahmed.
A common misconception is that % represents exactly one character. It actually represents any sequence of characters, including no characters; the underscore wildcard _ is generally used to represent exactly one character.
For an IB Computer Science A3.3 database programming question, write the complete SELECT, FROM, and WHERE clauses and place % correctly inside the quoted pattern. Examiners may ask you to construct a query or identify which records a given LIKE pattern returns.