SQL LIKE, IN and BETWEEN
LIKE, IN and BETWEEN are short ways to write common WHERE conditions.
LIKE: Search for a Pattern
LIKE compares text with a pattern. Two wildcards stand for unknown characters:
| Wildcard | Meaning |
|---|---|
% | Any number of characters (zero, one or many) |
_ | Exactly one character |
Names that start with "A":
Example
SELECT name
FROM students
WHERE name LIKE 'A%';| name |
|---|
| Ali Khan |
| Ayesha Siddiqui |
Names that contain "Iq" anywhere:
Example
SELECT name
FROM students
WHERE name LIKE '%Iq%';| name |
|---|
| Ayesha Siddiqui |
| Sana Iqbal |
"Siddiqui" also matches, because it contains the letters "iq". In MySQL and SQL Server LIKE usually ignores upper and lower case. In PostgreSQL it does not, and you would use ILIKE to ignore case.
Names whose second letter is "a":
Example
SELECT name
FROM students
WHERE name LIKE '_a%';| name |
|---|
| Fatima Noor |
| Hamza Raza |
| Sana Iqbal |
More LIKE Patterns
| Pattern | Matches |
|---|---|
'K%' | Starts with K |
'%n' | Ends with n |
'%an%' | Contains "an" |
'_____' | Exactly five characters |
'A%n' | Starts with A and ends with n |
Use NOT LIKE to find values that do not match.
IN: Match Any Value in a List
IN is a short way to write several OR conditions on the same column:
Example
SELECT name, city
FROM students
WHERE city IN ('Lahore', 'Islamabad');| name | city |
|---|---|
| Ayesha Siddiqui | Lahore |
| Fatima Noor | Islamabad |
| Sana Iqbal | Lahore |
This is the same as WHERE city = 'Lahore' OR city = 'Islamabad', but shorter. NOT IN gives the opposite.
IN with a Subquery
The list can come from another query. These are the courses that have at least one student:
Example
SELECT title
FROM courses
WHERE id IN (SELECT course_id FROM students);| title |
|---|
| Web Development |
| Python Programming |
| Graphic Designing |
Digital Marketing is missing because no student has course_id 4.
BETWEEN: Pick a Range
BETWEEN selects values inside a range. Both ends are included.
Example
SELECT name, age
FROM students
WHERE age BETWEEN 19 AND 21;| name | age |
|---|---|
| Ali Khan | 19 |
| Ayesha Siddiqui | 21 |
| Fatima Noor | 20 |
NOT BETWEEN returns the values outside the range:
Example
SELECT name, marks
FROM students
WHERE marks NOT BETWEEN 70 AND 90;| name | marks |
|---|---|
| Bilal Ahmed | 64 |
| Fatima Noor | 92 |
BETWEEN also works with text and dates, for example WHERE join_date BETWEEN '2026-01-01' AND '2026-06-30'.
NOT IN has a trap: if the list contains a NULL, it returns no rows at all. When the list comes from a subquery, add WHERE column IS NOT NULL inside the subquery to be safe.
Exercise
Exercise
Which condition finds names that end with the letter "r"?
